[英]Hibernate calling get() after save() on the newly created record within the same transaction
我正在使用沒有spring的hibernate 3.2.7(不要問,客戶端不支持spring),而且我遇到了open-session-in-view模式的實現問題。 當我通過調用save()將對象持久化到數據庫時,然后在該對象上調用get()以加載由數據庫中的外鍵表示的子對象的詳細信息。 問題是,當我調用get()時,沒有任何子對象被加載。 如果我從新事務中調用get()方法,則所有內容都按預期加載。
這是我的請求過濾器的一個片段,用於打開會話並創建事務:
HibernateUtil.openSession();
//get a transaction from JTA
transaction = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
transaction.begin();
// Call the next filter (continue request processing)
chain.doFilter(request, response);
// Commit and cleanup
log.finer("Committing the database transaction");
transaction.commit();
以下是服務層保存方法的片段,用於保存事務:
session.setFlushMode(FlushMode.MANUAL);
contract.save();
//save the update to the database
session.flush();
持久化對象后,請求將傳遞給struts操作類,該類調用服務中的get()方法,該方法具有此代碼以加載合同:
Session session = HibernateUtil.getSession();
session.setFlushMode(FlushMode.MANUAL);
try {
contract = contract.get();
...
}
加載持久合同時調用相同的get動作方法,該方法工作正常,因此我知道get()方法在自己的事務中被隔離時可以正常工作。 它不起作用的唯一時間是在同一事務中從save()之后調用它。
你試過對對象做一個Session.refresh()嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.