object references an unsaved transient instance - save the transient instance before,

Hi, there!

I am using hibernate and spring 2, and its UI framework is struts2.

I have an Item class, it has association with User and Category. User could sell item, so item has an foreign key from user. Everything goes fine in unit test, so it is not hbm configuration error. The problem is in struts 2, I got user from Httpsession, not from hibernate session. So it takes user as an transient instance. Why in unit test, user is attached to hibernate session. so it will not try to save user at all. How to attach user to session in struts 2 or in similar situation(I might use struts 1.3).  

In Unit test:
=====================================Unit test=========================================
public void saveItem() throws Exception {
            TransactionSynchronizationManager.
            bindResource(getSessionFactory(),new SessionHolder(getSession()));
            getSession().beginTransaction();
            Long userid = new Long(5);
            User user = (User) getHibernateTemplate().load(User.class, userid);
            Item item = new Item();
            item.setSellingUser(user);
      Category cat = (Category) getHibernateTemplate().load(Category.class, new Long(4));
            item.addCategory(cat);
            item.setName("petri");
            item.setDescription("verygood one");
            item.setInitprice(40);
            getHibernateTemplate().save(item);      
            getHibernateTemplate().flush();
            getSession().getTransaction().commit();
            TransactionSynchronizationManager.unbindResource(getSessionFactory());      
      }
====================================================================================
In struts 2:
==================================================================================
public String execute(){
            Item item = new Item();
            for (int i=0;i<cid.size();i++){
                  System.out.println("cid "+i+" is"+cid.get(i));
                  item.addCategory(getRegistrationService().getCategoryById(new Long(cid.get(i))));
            }      
            
            User user = (User) getSession().get("user");
            System.out.println("user id is "+user.getUid());            
            item.setSellingUser(user);
            item.setName(name);
            item.setDescription(description);
            item.setInitprice(new Integer(initPrice));
            getRegistrationService().insertItem(item);
            return SUCCESS;
      }
====================================================================================

Juhani
JianJunShenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
sounds like you need open session in view

http://www.hibernate.org/43.html
0
 
JianJunShenAuthor Commented:
object: Thanks for your answer.
But in web.xml, I already have following filters, In http://www.hibernate.org/43.589.html it said openSessionInViewFilter open a session or bind to current session when there is new view:
<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
   
     <filter>
     <filter-name>filterDispatcher</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

<filter-mapping>
     <filter-name>openSessionInViewFilter</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>
      
<filter-mapping>
    <filter-name>filterDispatcher</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
0
 
JianJunShenAuthor Commented:
Objects:

Session is one of the reason. Using session.lock(..) or session.update(...) to reattached objects in new session is the solution.

0
All Courses

From novice to tech pro — start learning today.