如何在Hibernate中更新Collection?

zpqajqem  于 6个月前  发布在  其他
关注(0)|答案(4)|浏览(72)

我有一个名为“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的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;
}

idfiyjo8

idfiyjo81#

试试这个

@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;
}

字符串
使用级联更新子Map

e4yzc0pl

e4yzc0pl2#

在Document实体类中使用cascade。

@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;
}

字符串
`然后当您保存或更新集合时,当您保存或更新文档实体时。

wswtfjt7

wswtfjt73#

在您的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;
}

g2ieeal7

g2ieeal74#

一个老问题,但我今天遇到了这个问题。我们使用的是一个非常旧的Hibernate版本(5.4.x),所以我不知道这与新版本有多大关系。
下面的方法对我很有效:

  • orphanRemoval = truecascade = CascadeType.ALL一起添加到@OneToMany注解中。这将删除缺少的行。
  • 使用Session.merge(parentObject),其中parentObject是本例中的文档。需要正确处理现有行。
  • 确保现有的DocumentPersonCc对象的@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;
}

字符串
文档人员抄送类:

@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

相关问题