我有一个Struts1.3和Hibernate 3.1应用程序,它使用open-session-in-view模式来维护hibernate事务。在我对一个对象执行session.save()之后,它的标识符被设置在对象中,但是在我将该对象传递给一个新的操作类之后,hibernate管理的所有属性,比如对象的标识符都被设置为null。
客户端没有使用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中,当我获得供应商属性时,所有标识符都设置为null:
Vendor vendor = (Vendor)dynaActionForm.get(VENDOR_PROPERTY_NAME); //vendorid is now null当持久性对象通过dynActionForm中的属性从一个操作类传递到另一个操作类时,为什么它会丢失标识符?
发布于 2011-09-15 01:11:01
这里的技巧是在getVendor服务方法中放置session.refresh():
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);
}
....
}https://stackoverflow.com/questions/7335452
复制相似问题