虽然我对此做了一些研究,但我发现在互联网上开了很多威胁,我无法解决我的问题。我附上我的代码:
Triple.java 主键由三个不同概念的3个URI组成,但它是同一列(这是一个问题吗?)
@Entity
@IdClass(ConceptPk.class)
@Table(name = "triple")
public class TripleDBModel {
protected List<Annotation> annotations;
public String conceptUriSubject;
public String conceptUriObject;
public String conceptUriPredicate;
public String id;
@ManyToMany(
cascade={CascadeType.ALL},
fetch=FetchType.LAZY
)
@JoinTable(name = "triple_has_annotation",
joinColumns=@JoinColumn(name="annotation_id"),
inverseJoinColumns={@JoinColumn(name="uri_concept_subject", referencedColumnName="triple_id"), @JoinColumn(name="uri_concept_object", referencedColumnName="triple_id"), @JoinColumn(name="uri_concept_predicate", referencedColumnName="triple_id")
})//EDIT
public List<Annotation> getAnnotations() {
return annotations;
}
public void setAnnotations(List<Annotation> annotations) {
this.annotations = annotations;
}
@Id
@Column(name = "uri_concept_subject", length = 100)
public String getConceptUriSubject() {
return conceptUriSubject;
}
public void setConceptUriSubject(String conceptUriSubject) {
this.conceptUriSubject = conceptUriSubject;
}
@Id
@Column(name = "uri_concept_object", length = 100)
public String getConceptUriObject() {
return conceptUriObject;
}
public void setConceptUriObject(String conceptUriObject) {
this.conceptUriObject = conceptUriObject;
}
@Id
@Column(name = "uri_concept_predicate", length = 100)
public String getConceptUriPredicate() {
return conceptUriPredicate;
}
public void setConceptUriPredicate(String conceptUriPredicate) {
this.conceptUriPredicate = conceptUriPredicate;
}
@Id
@Column(name = "triple_id", unique = true, nullable = false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
ConceptPk.java
@Embeddable
public class ConceptPk implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public String conceptUriSubject;
public String conceptUriObject;
public String conceptUriPredicate;
@Id
@Column(name = "uri", length = 100, unique = true, nullable = false)
public String getConceptUriSubject() {
return conceptUriSubject;
}
public void setConceptUriSubject(String conceptUriSubject) {
this.conceptUriSubject = conceptUriSubject;
}
@Id
@Column(name = "uri", length = 100, unique = true, nullable = false)
public String getConceptUriObject() {
return conceptUriObject;
}
public void setConceptUriObject(String conceptUriObject) {
this.conceptUriObject = conceptUriObject;
}
@Id
@Column(name = "uri", length = 100, unique = true, nullable = false)
public String getConceptUriPredicate() {
return conceptUriPredicate;
}
public void setConceptUriPredicate(String conceptUriPredicate) {
this.conceptUriPredicate = conceptUriPredicate;
}
Annotation.java
@Entity
@Table(name = "annotations")
public class Annotation {
private Integer id;
private List<TripleDBModel> triples;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "annotation_id", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToMany(
cascade={CascadeType.ALL},
mappedBy = "annotations", //EDIT
fetch=FetchType.LAZY
)
public List<TripleDBModel> getTriples() {
return triples;
}
public void setTriples(List<TripleDBModel> triples) {
this.triples = triples;
}
但是我收到了这个错误:
Caused by: org.hibernate.AnnotationException: A Foreign key refering TripleDBModel from Annotation has the wrong number of column. should be 3
我做错了什么?提前致谢
答案 0 :(得分:2)
最终的解决方案是:
ConceptPk.java
@Embeddable
public class ConceptPk implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public Concept conceptUriSubject;
public Concept conceptUriObject;
public Concept conceptUriPredicate;
@Id
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="uri_concept_subject")
public Concept getConceptUriSubject() {
return conceptUriSubject;
}
public void setConceptUriSubject(Concept conceptUriSubject) {
this.conceptUriSubject = conceptUriSubject;
}
@Id
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="uri_concept_object")
public Concept getConceptUriObject() {
return conceptUriObject;
}
public void setConceptUriObject(Concept conceptUriObject) {
this.conceptUriObject = conceptUriObject;
}
@Id
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="uri_concept_predicate")
public Concept getConceptUriPredicate() {
return conceptUriPredicate;
}
public void setConceptUriPredicate(Concept conceptUriPredicate) {
this.conceptUriPredicate = conceptUriPredicate;
}
Triple.java
@Entity
@IdClass(ConceptPk.class)
@Table(name = "triple")
public class TripleDBModel {
protected List<Annotation> annotations;
public Concept conceptUriSubject;
public Concept conceptUriObject;
public Concept conceptUriPredicate;
@ManyToMany(
cascade={CascadeType.ALL },
fetch=FetchType.LAZY
)
@JoinTable(name = "triple_has_annotation",
joinColumns={@JoinColumn(name="uri_concept_subject"), @JoinColumn(name="uri_concept_object"), @JoinColumn(name="uri_concept_predicate") },
inverseJoinColumns=@JoinColumn(name="annotation_id") )
public List<Annotation> getAnnotations() {
return annotations;
}
public void setAnnotations(List<Annotation> annotations) {
this.annotations = annotations;
}
@Id
@Column(name = "uri_concept_subject", length = 100)
public Concept getConceptUriSubject() {
return conceptUriSubject;
}
public void setConceptUriSubject(Concept conceptUriSubject) {
this.conceptUriSubject = conceptUriSubject;
}
@Id
@Column(name = "uri_concept_object", length = 100)
public Concept getConceptUriObject() {
return conceptUriObject;
}
public void setConceptUriObject(Concept conceptUriObject) {
this.conceptUriObject = conceptUriObject;
}
@Id
@Column(name = "uri_concept_predicate", length = 100)
public Concept getConceptUriPredicate() {
return conceptUriPredicate;
}
public void setConceptUriPredicate(Concept conceptUriPredicate) {
this.conceptUriPredicate = conceptUriPredicate;
}
}
答案 1 :(得分:1)
我不确定这是您收到的错误消息的原因,但您的映射有几个错误:
@JoinColumn
属性中的每个inverseJoinColumns
都应指定其referencedColumnName
属性。mappedBy
属性应为annotations
,而不是annotation
cascade=CascadeType.ALL
可能不是你想要的多对多。删除此三元组时,您不希望删除三元组的所有注释,因为这些注释会被其他三元组引用。答案 2 :(得分:0)
@ManyToMany(
cascade={CascadeType.ALL},
mappedBy = "annotation",
fetch=FetchType.LAZY
)
public List<TripleDBModel> getTriples() {
return triples;
}
mappedBy
应为“注释 s ”
编辑:查看this wikibook,尤其是底部。这是关联映射等的非常好的参考。