-->
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: 2+ references to the same entity id in session.
PostPosted: Tue Jan 18, 2005 5:16 pm 
Newbie

Joined: Tue Jan 18, 2005 5:12 pm
Posts: 2
I'm developing a webapp with Hibernate 2.1.7 + Spring, using OpenSessionInView.

We have this situation: object A has references to object B and C.
Both objects B and C have references to an object X. And B has a Set of C objects..
All hashCode and equals methods necessary are write. The mapping files are ok, only using common many-to-one
and inverse sets..

Code:
class A {
    B b;
    C c;
    X x;
    // ...
}

class B {
    X x;
    Set cees;
    // ...
}

class C {
    X x;
    B b;
    // ...
}


In one case, X has the same entity identifier in the session for A, B and C (but not the same instances!!).
When we go to saveOrUpdate new instance of A, the Hibernate throws NonUniqueObjectException. To avoid
NonUniqueObjectException, I'm trying this, but them the NonUniqueObjectException occurs with C.b id!!!

Code:
class A_DAO {
public void saveA(A a) {
    // First:
    if (a.b.x.equals(a.c.x)) {
        a.b.x = a.c.x; // same entity id and same instance reference...so it's ok to save..
    }
    // Second!!!
    if (a.x.equals(a.b.x)) {
        a.x = a.b.x; // same entity id and same instance reference...so it's ok to save..
    }
   
    // Last, but here LazyLoadException
    if (a.b.equals(a.c.b)) {
        a.c.b = a.b;
    }
   
    getHibernateTemplate().saveorUpdate(a);
}
}


What's the correct approach for this situation? It's wrong(?) and ugly to write lot's of code in the DAO's for this
cases and my model has "n" situations where multiple references to the same entity id could occurs..

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 19, 2005 3:08 am 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
It's true that a given entity can only exist in a given Session once. To get around this, you may try one of two things:

1) Use session.saveOrUpdateCopy(), which will update the session's state with information from the pojo you pass in,

or

2) Evict the object before updating it. That will assure that there is no object in the session at the time of the update.

You may want to re-evaluate your architecture to see if multiple sessions per operation are really necessary. Consider the ThreadLocal or Open Session in View patterns (search the Hibernate site for more info) to only use a single Session.

One thing that many people fall into the trap of is that they believe that session.update() will update the database with changes in their object. While it's true that this will eventually happen, the real definition of "update" as far as Hibernate is concerned is that update() tells Hibernate to update the state of the session with the object being passed in. You're not "updating" the database, you're "updating" Hibernate. Very distinct difference. Effectively, the Update statement just tells hibernate to reassociate the session with your PoJo. If the object in question is already associated to the session, a call to session.update() will do nothing. You could just as easily make changes to the object and make no calls to Hibernate and the object's persistent state will still be stored in the DB when the Hibernate session.flush() method is called (probably at the end of a transaction - check your demarcation to find out when saves really happen.)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 19, 2005 8:04 am 
Newbie

Joined: Tue Jan 18, 2005 5:12 pm
Posts: 2
Ok, thanks for you reply!

I'm using OpenSessionInView pattern, and each operation uses only one Session.

I will work with saveOrUpdateCopy to see wath happens...


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.