我有一个名为“Document”的对象,它与“DocumentPersonCc”具有一对多关系。我正在尝试更改代码中的一个或多个集合,但 Hibernate 没有在我期望的时候调用更新查询。当我更改常规字段之一(如文档主题)时,它确实会调用更新查询,但即使如此,它也不会保留数据库中的任何一对多更改。我需要更改什么才能使其正常工作?
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;
}
更新代码:
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 的文档 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;
}
试试这个 -
@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;
}
使用级联更新子映射
在文档实体类中使用级联。 `
@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;
}
` 然后当你保存或更新集合时,当你保存或更新文档实体时。
在 Document.java 中添加一个方法作为
public void addDocumentPersonCc(DocumentPersonCc documentPersonCc) {
documentPersonCc.setDocument(this);
documentPersonCcs.add(documentPersonCc);
}
现在将 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;
}
一个老问题,但我今天几乎遇到了这个问题。我们使用的是非常旧的 Hibernate 版本 (5.4.x),所以我不知道这与新版本有多大相关性。
以下对我有用:
orphanRemoval = true
与 @OneToMany
一起添加到 cascade = CascadeType.ALL
注释中。这就是删除缺失行的原因。Session.merge(parentObject)
,其中 parentObject
是本例中的文档。需要正确处理现有行。@Id
属性设置正确,以防止重复插入。我必须编写一些逻辑来识别输入中的这些内容并设置正确的 ID,但这相当简单。它不包含在下面的代码中。文档类:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "document", orphanRemoval = true)
@LazyCollection(LazyCollectionOption.EXTRA)
@Sort(type = SortType.NATURAL, comparator = DocumentPersonCc.class)
public SortedSet<DocumentPersonCc> getDocumentPersonCcs() {
return this.documentPersonCcs;
}
DocumentPersonCc 类:
@Id
//Other Hibernate annotations...
public int getDocumentPersonCcId() {
return this.documentPersonCcId;
}
更新方法:
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());
//The changed rows:
oldD.getDocumentPersonCcs().clear();
oldD.getDocumentPersonCcs().addAll(document.getDocumentPersonCcs());
session.merge(oldD);
session.getTransaction().commit();
document = this.returnDocument(document.getId());
Hibernate.initialize(document.getDocumentPersonCcs());
return document;
}
orphanRemoval
属性描述于here。