java 如何在 Hibernate 中更新集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11219799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I update a Collection in Hibernate?
提问by J Ellis
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?
我有一个名为“Document”的对象,它与“DocumentPersonCc”是一对多的关系。我试图在我的代码中更改这些集合中的一个或多个,但是 Hibernate 没有在我期望的时候调用更新查询。当我更改常规字段之一(如文档主题)时,它确实会调用更新查询,但即使如此,它也不会保留数据库中的任何一对多更改。我需要改变什么才能让它工作?
Getter and setter for DocumentPersonCc:
DocumentPersonCc 的 getter 和 setter:
@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:
DocumentPersonCc 的 Document getter 和 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;
}
回答by Pramod Kumar
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
使用级联更新子映射
回答by Niroshan Abayakoon
use cascade in your Document entity class. `
在您的 Document 实体类中使用级联。`
@OneToMany(fetch = FetchType.LAZY, mappedBy = "document")
@LazyCollection(LazyCollectionOption.EXTRA)
@Sort(type = SortType.NATURAL, comparator = DocumentPersonCc.class)
@Cascade({CascadeType.SAVE_UPDATE})
public SortedSet<DocumentPersonCc> getDocumentPersonCcs() {
return this.documentPersonCcs;
}
` Then when you save or update collection when you save or update document entity.
` 然后当您保存或更新集合时保存或更新文档实体。
回答by Rahul Agrawal
In your Document.java add one method as
在你的 Document.java 添加一个方法作为
public void addDocumentPersonCc(DocumentPersonCc documentPersonCc) {
documentPersonCc.setDocument(this);
documentPersonCcs.add(documentPersonCc);
}
Now change your updateCcs method as
现在将您的 updateCcs 方法更改为
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());
SortedSet<DocumentPersonCc> documentPersonCcsList = document.getDocumentPersonCcs();
//Iterate this documentPersonCcsList
{
// add oldD.addDocumentPersonCc(Value from Iterator);
}
session.update(oldD);
session.getTransaction().commit();
document = this.returnDocument(document.getId());
Hibernate.initialize(document.getDocumentPersonCcs());
return document;
}