簡體   English   中英

Hibernate被管理對象的ID在轉發到新的ActionClass之后被重置

[英]Hibernate managed Object's ID being reset after being forwarded to new ActionClass

我有一個Struts 1.3和Hibernate 3.1應用程序,該應用程序使用開放會話模式來維護休眠事務。 在對象上執行session.save()之后,將在對象中設置其標識符,但是,在將對象傳遞給新的動作類之后,將所有由休眠管理的屬性(例如對象的標識符)設置為空值。

客戶端沒有使用Spring,因此我必須編寫自己的模式的請求過濾器實現,如下所示:

//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();

我的SaveActionClass調用服務層來持久化對象及其關聯的列表(我在其中控制會話)如下所示:

this.saveAddresses(vendor); //saves a persistant set to the database via dao

this.saveExpCodes(vendor, expCodes); //saves a persistant set to the database  via dao

this.savePhoneNumbers(vendor); //saves a persistant set to the database  via dao

vendor.save(); //saves the vendor object to the database via dao

session.flush();  

session.refresh(vendor);

供應商對象保留之后,供應商對象及其所有子對象具有有效的標識符。 然后將供應商對象添加到DynActionForm屬性,然后轉發到ViewActionClass:

dynaActionForm.set(VENDOR_PROPERTY_NAME, vendor);

return actionMapping.findForward(target); //viewvendor

然后在ViewActionClass中,當我獲得vendor屬性時,所有標識符都設置為null:

Vendor vendor = (Vendor)dynaActionForm.get(VENDOR_PROPERTY_NAME); //vendorid is now null

當持久對象通過dynActionForm中的屬性從一個動作類傳遞到另一個動作類時,為什么會丟失其標識符?

這里的技巧是將session.refresh()放置在getVendor服務方法中:

public Vendor getVendor(Vendor vendor, Boolean refresh) {
    Session session = HibernateUtil.getSession();

    session.setFlushMode(FlushMode.MANUAL);

        //refreshing the session here before the get call gave me the would still set some properties to null
        /*
        if(refresh.booleanValue()){
        session.refresh(vendor);
    }
    */

    vendor = vendor.get();

        //however putting the refresh here, after the get() call populated the vendor object properly
    if(refresh.booleanValue()){
        session.refresh(vendor);
    }
        ....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM