2012-06-27 137 views
2

我有一個名爲「Document」的對象,它與「DocumentPersonCc」具有一對多的關係。我試圖在我的代碼中更改這些集合中的一個或多個,但是當我期望Hibernate沒有調用更新查詢時。當我更改一個常規字段(如文檔主題)時,它會調用更新查詢,但即使這樣,它也不會持續數據庫中的任何一對多更改。我需要更改什麼才能使其正常工作?如何更新Hibernate中的集合?

getter和setter的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; 
} 

更新代碼:

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

回答

3

試試這個 -

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

使用級聯更新子映射

0

使用級聯。 `

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

`然後當您保存或更新集合時保存或更新文檔實體。

+0

太棒了!這完美地工作,除了如果更新的集合包含已經在數據庫中的條目,我會得到一個異常:「org.hibernate.NonUniqueObjectException:具有相同標識符值的不同對象已經與會話相關聯。」我試着在Document.DocumentPersonCcs上做一個「clear()」,更新它,然後設置新的集合並再次更新它,但是我仍然得到相同的錯誤。我是否必須遍歷並刪除每個項目,還是有更簡單的方法來完成它? –

+0

爲了澄清上述評論,我想完全用新集合取代舊集合。設置將添加新集合中舊集合中不存在的元素,但會對兩個元素中存在的元素引發異常,並且不會刪除舊集合中存在但不存在於新集合中的元素。 –

+0

那麼你必須使用merge()方法而不是更新。因爲如果賽季包含來自實體的多個對象,我們必須合併這些對象。 –

0

在你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; 
}