Advertisement

07.27.2007 at 05:13AM PDT, ID: 22725147
[x]
Attachment Details

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

Asked by JianJunShen in Java Programming Language, J2EE

Tags: unsaved, references, save, flushing

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;
      }
====================================================================================

JuhaniStart Free Trial
[+][-]07.27.2007 at 05:47AM PDT, ID: 19581807

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, J2EE
Tags: unsaved, references, save, flushing
Sign Up Now!
Solution Provided By: objects
Participating Experts: 1
Solution Grade: C
 
 
[+][-]07.27.2007 at 06:40AM PDT, ID: 19582149

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.30.2007 at 05:34PM PDT, ID: 19596961

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32