-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many mappings deleted on evict
PostPosted: Thu Apr 22, 2004 6:53 am 
Newbie

Joined: Thu Apr 22, 2004 6:30 am
Posts: 4
Hi,

I have a 'service' object which is associated with a set of 'availabilty levels' objects in a many-to-many mapping:

Code:
<set name="availabilityLevels" lazy="false" table="AVAILABILITY_LEVEL" inverse="false" cascade="all" sort="unsorted">
  <key column="SERVICE_FK"/>
  <many-to-many class="com...AvailabilityLevel" column="AVAILABILITY_LEVEL_FK" outer-join="auto"/>
</set>

The following code causes the association between the requested service (identified by PK) and its applicable availability levels to be deleted:

Code:
public Service findServiceByKey(String key) {
  Service service = null;
  try {
    Session session = getSession(); // retrieve a session maintained by ThreadLocal
    Service service = (Service) session.load(Service.class, key); // load the service
    session.evict(service); // evict it from the session - this causes the associations to be deleted!
  }
  catch (HibernateException he) {
    ...
  }

  return service;
}

After this method has been invoked and the transaction in which it was participating has succesfully completed, the associations are gone! Removing the call to evict the object from the session fixes this problem (i.e. the service <-> availability level assocations are maintained), but I'd really like to understand why evicting the service object causes this problem (before I start coding a work-around).

Cheers,
James.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 6:57 am 
Newbie

Joined: Thu Apr 22, 2004 6:30 am
Posts: 4
Sorry, I forgot to mention that I'm using Hibernate version 2.0.2 (if it makes any difference!)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 4:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You're not showing the right test case.
What I suspect is that you do

Code:
service = session.load(...);
service.getAvailLevel).add(level); //add it
session.evict(service); //removed from the session before any flush so the association is not created

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 6:57 am 
Newbie

Joined: Thu Apr 22, 2004 6:30 am
Posts: 4
No, this is the correct test case, i.e. a read-only operation to retrieve a service and its associated availability levels. The associations between the service that is being retrieved and its availability levels already exist. That is, I'm not adding new availability levels, simply querying existing ones.

In an earlier version of the method given, the call that evicts the loaded service object did not exist and everything worked as expected (no associations in the link table were lost). Another developer later added the call to evict the object which caused this funny behaviour.

I understand why the test case that you gave would not work as it is being cached locally and not being given a chance to synchronize with the database. However, I'm at a loss to explain why evicting the 'service' object after it has been loaded for reading would cause this to happen.

Cheers,
James.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 12:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
reproduce in a simple runnable test case.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.