-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: illegally attempted to associate a proxy with two open Sessi
PostPosted: Wed Sep 21, 2005 12:03 pm 
Newbie

Joined: Tue Sep 20, 2005 11:20 am
Posts: 5
I am having a horrible time with hibernate updating the database of an existing record. I am using struts, and have an action form and a value object. When I first open a record from the database, I create my value object. Here is my code in my DAO for getting the record.
Code:
Incident incident = (Incident) HibernateUtil.getSession().load(Incident.class, rbNumber);
        Victim victim = (Victim) HibernateUtil.getSession().createQuery(
                "from org.dotcomm.opd.valueobjects.Victim victim where victim.rbNumber = :rbNumber").setString("rbNumber", incident.getRbNumber())
                .uniqueResult();
        MissingPerson missingPerson = (MissingPerson) HibernateUtil.getSession().createQuery(
                "from org.dotcomm.opd.valueobjects.MissingPerson mp where mp.victim = :victimObj").setEntity("victimObj", victim).uniqueResult();
        Offense offense = (Offense) HibernateUtil.getSession().createQuery(
                "from org.dotcomm.opd.valueobjects.Offense offense where offense.rbNumber = :rbNumber").setString("rbNumber", incident.getRbNumber())
                .uniqueResult();
HibernateUtil.commitTransaction();


I then create my action form for struts from this value object, let the user update the information, and then when they hit save, I update the value object. I then try to save the value object using this code.
Code:
HibernateUtil.getSession().update(missingPerson.getIncident());

                HibernateUtil.getSession().update(missingPerson.getOffense());
                //HibernateUtil.getSession().update((NCICInformation)missingPerson.getOffense().getNcicInformations().iterator().next());

                HibernateUtil.getSession().update(missingPerson);
                HibernateUtil.getSession().update(missingPerson.getVehicle());

            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

It throws the Exception org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
on the first update of the line. I understand that I created a session when I queried the objects. All I want to do is update them. How do I clear out the old session, or re-use it, or what do I need to do here?

If I close the session after the query, I get a lazyloading exception that a session doesn't exist. If I try to clear the session before updating the classes, it has no effect. Can someone tell me what I am missing? Thanks.


Top
 Profile  
 
 Post subject: bump
PostPosted: Wed Sep 21, 2005 2:52 pm 
Newbie

Joined: Tue Sep 20, 2005 11:20 am
Posts: 5
bumping this up.... somebody should know this... Please help


Top
 Profile  
 
 Post subject: ok figured it out. Is this the right way????
PostPosted: Wed Sep 21, 2005 4:42 pm 
Newbie

Joined: Tue Sep 20, 2005 11:20 am
Posts: 5
I made some changes which made this work.

When I did my original query, I needed to close the Hibernate session. I tried that before and was getting an error, however, because lazyloading was on, and many of the attributes were accessed AFTER the DAO ran, (namely when I was copying from the value object to the action form). I could not turn off lazy loading even when I set lazy loading to false. So what I did is created the Hybernate Session in my Action Class, passed it to my service, which then passed it to the DAO. Then when my action class got the value object back, it could copy the object to the action form and THEN close the session.

Evidently in my next trip to the server, HibernateUtil.getSession() got a different session, and not the session that was created on my query. Because the session in the query was still "floating around", when I created a new one, and tried to update the object it caused a problem. This brings me to another question.

How do you reference an existing Hibernate Session that may still be active from a past trip to the server, or do you have to explicitely store the hibernate session in an http session, or in a scope available to the user in each trip.

Also, Isn't HibernateUtil a singleton which should pass you the one Hybernate session active for your user thread in the application?

Thanks in advance for any good feed back.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.