我正在建立数据库模型。
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;
    }
}


三元组

@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;
    }
}


提前致谢!!

最佳答案

您可以使用如下的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"), ... }

10-02 08:10