I’m having some problems using cascade operations on hibernate. I have one entity (TripleDBmodel), the primary key of the entity is created using references to a three objects (Concept). It is working perfect, I also added cascade operations on these objects, I thougth that save operation for TripleDBModel entity will save also Concepts that does not exist in database. However it is not working like that.
@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;
}
....
}
The cascade option only works on the @OneToMany side of the relationship.
You should know when you need to create a new concept anyway so persisting it separately should not be a problem?