I have an object called "Document" that has a one-to-many relationship with "DocumentPersonCc". I am trying to change one or more of these collections in my code, but Hibernate is not calling the update query when I expect it. It does call the update query when I change one of the regular fields (like the document subject), but even then it does not persist any of the one-to-many changes within the database. What do I need to change in order to get it to work?
Getter and setter for DocumentPersonCc:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "document")
@LazyCollection(LazyCollectionOption.EXTRA)
@Sort(type = SortType.NATURAL, comparator = DocumentPersonCc.class)
public SortedSet<DocumentPersonCc> getDocumentPersonCcs() {
return this.documentPersonCcs;
}
public void setDocumentPersonCcs(
SortedSet<DocumentPersonCc> documentPersonCcs) {
this.documentPersonCcs = documentPersonCcs;
}
Update Code:
public Document updateCcs(Document document) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Document oldD = (Document) session.load(Document.class, document.getId());
Hibernate.initialize(oldD.getDocumentPersonCcs());
oldD.setDocumentPersonCcs(document.getDocumentPersonCcs());
session.update(oldD);
session.getTransaction().commit();
document = this.returnDocument(document.getId());
Hibernate.initialize(document.getDocumentPersonCcs());
return document;
}
DocumentPersonCc's Document getter and setter:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "DocumentsId", nullable = false, insertable = false, updatable = false)
protected Document getDocument() {
return this.document;
}
public void setDocument(Document document) {
this.document = document;
}
Try this -
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "document")
@LazyCollection(LazyCollectionOption.EXTRA)
@Sort(type = SortType.NATURAL, comparator = DocumentPersonCc.class)
public SortedSet<DocumentPersonCc> getDocumentPersonCcs() {
return this.documentPersonCcs;
}
Use cascade to update child mapping