2010-11-17 84 views
0

我有一個實體類Document和另一個名爲空間。關係:JPA堅持對象沒有調用persist

@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, 
     CascadeType.MERGE, CascadeType.REFRESH}, optional = true) 
@ForeignKey(name = "FK_TO_SPACE__DOCUMENT") 
@IndexedEmbedded(prefix = DocumentDefaultFields.SPACE_TO_PREFIX) 
private Space toSpace; 

嗯,我查詢數據庫並採取一些文檔到一個LinkedList。

<a:commandLink value="move" action="#{moveDocsOperation.moveDocumentToNewSpace(entity)}" reRender="confim,origTable,newTable"/> 

和方法:

這個列表是從那裏我可以做這樣一些更新操作綁定到一個DataTable

public void moveDocumentToNewSpace(final Document document) { 
    log.info("~~move document #0 from space #1 to space #2", document.getDocumentId(), origSpace.getPath(), newSpace.getPath()); 
    document.setToSpace(newSpace); 
    origSpaceDocuments.remove(document); 
    newSpaceDocuments.add(document); 
    entityAuditer.auditBean(document, Crud.UPDATE); 
} 

我不明白爲什麼,設置的toSpace時文件實體,更新也在DB中完成,而實際上並沒有執行PERSIST ....

你知道嗎爲什麼?

回答

3

當您通過休眠會話加載對象時,它由該會話管理。當您進行更改時,在刷新時間內,對象中的更改將與數據庫同步。因此調用persist()不需要持久化數據修改。 (相關閱讀:http://techblog.bozho.net/?p=227),你可以避開這個問題,並進行更改實體沒有堅持到數據庫

+0

非常感謝。所以這意味着隱式FlushMode是AUTO? – 2010-11-17 10:04:17

+0

是的,它是AUTO .. – Bozho 2010-11-17 10:12:24

0

一種方法是從會話中刪除:

org.hibernate.Session session = (Session) em.getDelegate(); 
session.evict(yrEnity);