-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Add an extra colum on a intermediate table
PostPosted: Fri May 20, 2011 4:19 am 
Newbie

Joined: Wed Dec 29, 2010 8:58 am
Posts: 10
Hello, I have seen a lot of threads very similar to this one, bur neither of them could help me :s

I have two entities: triple and concept. A triple will contain 3 concepts, one of them object, subject or predicate.

Modeling in java:

Triple.java
Code:
    @Entity
    @Table(name = "triple")
    public class Triple {
    protected List<Concept> concepts; 
    @ManyToMany(
               cascade={CascadeType.ALL },   
               fetch=FetchType.LAZY
       )   
        @JoinTable(name = "triple_has_concept",
              joinColumns={@JoinColumn(name="triple_id")},        
              inverseJoinColumns=@JoinColumn(name="concept_id") )
       public List<Concept> getconcepts() {
          return concepts;
       }
       public void setconcepts(List<Concept> concepts) {
          this.concepts = concepts;
       }


Concept.java


Code:
@Entity
    @Table(name = "concept")
    public class Concept implements java.io.Serializable {
    private List<TripleDBModel> triple;
    @ManyToMany(
               cascade={CascadeType.ALL},
               mappedBy = "concepts",
               fetch=FetchType.LAZY
       )   
       public List<Triple> getTriple() {
          return triple;
       }
   
       public void setTriple(List<Triple> triple) {
          this.triple = triple;
       }


Notice that one concepts could be in one triple object, but in another predicate, for instance. Then I need to add an extra field, `conecptType` (can not be part of the `@Id` in Concept) to the `triple_has_concept` table. I tryed several things, like declare it as a foreign key, or create an extra table, but it would be much better to have all together. Any ideas??
Thanks in advance


Top
 Profile  
 
 Post subject: Re: Add an extra colum on a intermediate table
PostPosted: Mon May 23, 2011 2:45 pm 
Beginner
Beginner

Joined: Mon Jul 28, 2008 4:36 pm
Posts: 24
Solution 1:
If subjects, objects, and predicates are practically the same thing and you deal with them very similarly in your code, just add a "concept_type" field to your concept. In Java, declare an enum:
Code:
public class Concept {
    public enum Type {
        SUBJECT,
        OBJECT,
        PREDICATE;
    }

    // This is your database field
    private String typeStr;

    // Database field getters and setters are private so only Hibernate and this class can access them
    private String getTypeStr() { return typeStr; }
    private void setTypeStr(String t) { typeStr = t; }

    // All other classes must use the enum to set and read the type value:
    public Type getType() {
        if (typeStr == null) { return null; }
        return Concept.valueOf(typeStr);
    }
    public void setType(Type t) {
        if (t == null) {
            typeStr == null;
        } else {
            typeStr = t.toString();
        }
    }
}

Solution 2:
I'm guessing your object, subject, and predicates have some similarities, but also significant differences. Depending on what you are doing with your concept object, you may want to have 3 different types of objects: object, subject, and predicate. If they have similar fields, name those fields exactly the same, then make a "Concept" interface that contains those fields and make your three classes implement that interface. If you need to get a set from the Triple object, you can have:
Code:
public Set<Concept> getConcepts() {
    Set<Concept> ret = new HashSet<Concept>()
    if (object != null) {
        ret.add(object);
    }
    if (subject != null) {
        ret.add(subject);
    }
    if (predicate != null) {
        ret.add(predicate);
    }
    return ret;
}

You no longer need a triple_has_concept table. Rather:
Code:
triple
    id
    subject_id
    object_id
    predicate_id


You asked for other ideas and no-one else responded, so there are some ideas.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.