[英]Hibernate @id composite
I am modeling a database.我正在建模一个数据库。
There is TRIPLE
which contains three CONCEPT
.有包含三个
CONCEPT
TRIPLE
。 So the primary key of the class TRIPLE
is three uri
all together.所以
TRIPLE
类的主键是三个uri
。 (One concept could be in different TRIPLE
). (一个概念可能在不同的
TRIPLE
)。
Also TRIPLE
is related with another class, ANNOTATION
, and here is the question, how can triple_id
be identified??另外
TRIPLE
与另一个类ANNOTATION
,这里的问题是,如何识别triple_id
?? But first of all, if building this Id composite is correct.但首先,如果构建这个 Id 组合是正确的。 To model that:
要建模:
Concept.java概念.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三重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;
}
}
Thanks in advance!!提前致谢!!
You could use an Id class like this:您可以使用这样的 Id 类:
class TripleId implements Serializable {
@Column(...)
private String conceptUriSubject;
@Column(...)
private String conceptUriObject;
}
And use it in Triple:并在 Triple 中使用它:
@Entity
@Table(name = "triple")
public class TripleDBModel {
@EmbeddedId
private TripleId id;
...
}
Also note that you can provide multiple join columns: inverseJoinColumns= {@JoinColumn(name="subjectUri"), @JoinColumn(name="objectUri"), ... }
另请注意,您可以提供多个连接列:
inverseJoinColumns= {@JoinColumn(name="subjectUri"), @JoinColumn(name="objectUri"), ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.