Hibernate级联操作无法正常工作

时间:2012-04-05 08:23:33

标签: hibernate save

我在使用hibernate上的级联操作时遇到了一些问题。我有一个实体(TripleDBmodel),实体的主键是使用对三个对象(Concept)的引用创建的。它工作正常,我还在这些对象上添加了级联操作,我认为TripleDBModel实体的保存操作也将保存数据库中不存在的概念。然而它不是那样的。

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

private Concept subject;
private Concept object;
private Concept predicate;
public TripleDBModel(){

}
@Id
@ManyToOne(targetEntity=Concept.class)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name="subject_uri")
public Concept getSubject() {
    return subject;
}
public void setSubject(Concept subject) {
    this.subject = subject;
}
@Id
@ManyToOne(targetEntity=Concept.class)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name="object_uri")
public Concept getObject() {
    return object;
}
public void setObject(Concept object) {
    this.object = object;
}
@Id
@ManyToOne(targetEntity=Concept.class)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name = "predicate_uri")
public Concept getPredicate() {
    return predicate;
}
public void setPredicate(Concept predicate) {
    this.predicate = predicate;
}

}


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

private String uri;
private String label;


// private List<TripleDBModel> triple;

public Concept(String uri, String label) {
    this.uri = uri;
    this.label = label;
    this.ontologies = new ArrayList<Ontology>();
}

public Concept() {
    super();
}

@Id
@Column(name = "uri", length = 255, unique = true, nullable = false)
public String getUri() {
    return uri;
}

public void setUri(String uri) {
    this.uri = uri;
}
....

}

1 个答案:

答案 0 :(得分:0)

级联选项仅适用于关系的@OneToMany端。

你应该知道什么时候需要创建一个新的概念,所以单独坚持它应该不是问题?