Hibernate @id复合

时间:2011-04-12 10:44:24

标签: java hibernate

我正在为数据库建模 有TRIPLE,其中包含三个CONCEPT。因此,类TRIPLE的主键一起是三个uri。 (一个概念可能在不同的TRIPLE) 此外,TRIPLE与另一个类ANNOTATION相关,这是一个问题,如何识别triple_id?但首先,如果构建此Id复合词是正确的。 为此建模:

Concept.java

@Entity
@Table(name = "concept")
public class Concept implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private String id;
    private List<TripleDBModel> triples;  
@ManyToMany(
            cascade={CascadeType.ALL},  
            fetch=FetchType.LAZY, 
            mappedBy = "concepts"  
    )   
    public List<TripleDBModel> getTriples() {
        return triples;
    }

    public void setTriples(List<TripleDBModel> triples) {
        this.triples = triples;
    }

ConceptPk.java

@Embeddable
public class ConceptPk implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private String uri;

    public ConceptPk(String uri, String label){
        this.uri = uri;
    }

    public ConceptPk(){
        super();
    }   

    @Id
    @Column(name = "uri", length = 100, unique = true, nullable = false)
    public String getUri() {
        return uri;
    }
    public void setUri(String uri) {
        this.uri = uri;
    }
}

Triple.java

@Entity
@IdClass(ConceptPk.class)
@Table(name = "triple")
public class TripleDBModel {

    protected List<Annotation> annotations;
    protected String conceptUriSubject;
    protected String conceptUriObject;
    protected String conceptUriPredicate;



    @ManyToMany(
            cascade={CascadeType.ALL},  
            fetch=FetchType.LAZY
    )   
    @JoinTable(name = "triple_has_concept", 
            joinColumns=@JoinColumn(name="uri"),
            inverseJoinColumns=@JoinColumn(name="triple_id")) //What shoul I write here???
    public List<Annotation> getAnnotations() {
        return annotations;
    }
    public void setAnnotations(List<Annotation> annotations) {
        this.annotations = annotations;
    }
    @Id public String getConceptUriSubject() {
        return conceptUriSubject;
    }
    public void setConceptUriSubject(String conceptUriSubject) {
        this.conceptUriSubject = conceptUriSubject;
    }
    @Id public String getConceptUriObject() {
        return conceptUriObject;
    }
    public void setConceptUriObject(String conceptUriObject) {
        this.conceptUriObject = conceptUriObject;
    }
    @Id public String getConceptUriPredicate() {
        return conceptUriPredicate;
    }
    public void setConceptUriPredicate(String conceptUriPredicate) {
        this.conceptUriPredicate = conceptUriPredicate;
    }
}  

提前致谢!!

1 个答案:

答案 0 :(得分:2)

你可以使用这样的Id类:

class TripleId implements Serializable {
  @Column(...)
  private String conceptUriSubject;

  @Column(...)
  private String conceptUriObject;
}

并在Triple中使用它:

@Entity
@Table(name = "triple")
public class TripleDBModel {

  @EmbeddedId
  private TripleId id;
  ...
}

另请注意,您可以提供多个连接列: inverseJoinColumns= {@JoinColumn(name="subjectUri"), @JoinColumn(name="objectUri"), ... }