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

Posted on 2007-07-27
Last Modified: 2011-10-03
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 {
            bindResource(getSessionFactory(),new SessionHolder(getSession()));
            Long userid = new Long(5);
            User user = (User) getHibernateTemplate().load(User.class, userid);
            Item item = new Item();
      Category cat = (Category) getHibernateTemplate().load(Category.class, new Long(4));
            item.setDescription("verygood one");
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.setInitprice(new Integer(initPrice));
            return SUCCESS;

Question by:JianJunShen
    LVL 92

    Accepted Solution

    sounds like you need open session in view

    Author Comment

    object: Thanks for your answer.
    But in web.xml, I already have following filters, In it said openSessionInViewFilter open a session or bind to current session when there is new view:


    Author Comment


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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    twoTwo  challenge 35 71
    hash value 2 29
    the registry refers to a nonexistent java runtime environment 6 61
    changePi Challenge 15 55
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now