-->
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.  [ 2 posts ] 
Author Message
 Post subject: Transitive persistence and dreaded NotUniqueObjectException
PostPosted: Mon Oct 30, 2006 11:46 pm 
Newbie

Joined: Thu Feb 02, 2006 10:55 pm
Posts: 3
Hi,

I have a suspicion this subject has been BBQed many times, but can't find a definitive answer by searching through archives. Sorry for digging out buried corpse again if so.

Environment: Hibernate 3.2.0.ga + Spring 1.2.6 +Hsqldb 1.8.0.7

What I'm trying to accomplish is utterly trivial:

I have a class called Employee that has a many-to-many bidirectional relationship with a class called Position. All the code is trying to do is simple crud on these two objects. I use optimistic locking, but what to check whether object exists before deciding whether it is stale or not (for some reason Hibernate always says it is stale event if it is not there at all).

I almost hacked it into working by doing lots of code gymnastics that normally I would find quite unnecessary. Here are the issues:

1. Creation of a new Position with a few existing Employee objects throws
NotUniqueObjectException unless I loop through all the Employee object's and call 'load' on them prior to save.

This code throws NotUniqueObjectException :
Code:
(...)
this.getHibernateTemplate.save(newPosition);
(...)


while this works:
Code:
List employeeList = newPosition.getEmployees();

if (employeeList != null)
{
   Iterator iterator = employeeList.iterator();
   List reattachedEmployeeList = new ArrayList(employeeList.size());

   while (iterator.hasNext())
   {
      Employee employee = (Employee) iterator.next();

      try
      {
         employee = (Employee) this.getHibernateTemplate().load(Employee.class, employee.getId());
      }
      catch (Exception e)
      {
         //OK - no object in the session
      }

      reattachedEmployeeList.add(employee);
   }

newPosition.setEmployees(reattachedEmployeeList);
}

this.getHibernateTemplate.save(newPosition);

Question: Is it a bug or feaure? This was part of the gymnastics that I was referring to.

2. When I call 'session.load' on a non-existent object (i.e. one with a silly id and silly version) I still get an object back (sic!), but when I try to update or delete it (even more silly) I get optimistic locking exception saying that the object has been deleted. All I want to do is to detect that it does not exist.
Question: Again - bug or I'm doing something wrong? The object never existed so I should get either null or exception. Why is it locading a dodgy object?

3. NotUniqueObjectException is guaranteed to be thrown if I use 'session.get' on updated/created/deleted object or any related cascaded ones. Bascially anything touched by 'session.get' becomes cursed and cannot be saved/deleted. Using 'load' helps a bit, but then the issue described in p.2 occurrs.

This code throws NotUniqueObjectException on delete:

Code:
Long theId = thePosition.getId();
Object obj = this.getHibernateTemplate.get(Position.class,theId);

if (obj==null)
{
   throw new PositionNotFoundException();
}
else
{
  //I want to get rid of the instance loaded by get. No use though :-(
   this.getHibernateTemplate.evict(obj);
}

//This throws NotUniqueObjectException
this.getHibernateTemplate.delete(thePosition);

Question: What would be the correct Hibernate code to check for the existence of an object in the DB before calling update/delete?

4. session.evict and/or session.clear do not seem to be doing anything. I can evict or clear everything I want and I'll still be getting NotUniqueObjectExeption (see code example in p.3).

Is Hibernate full of bugs or am I full of bugs ;-).

Thanks in advance

Maciek


Top
 Profile  
 
 Post subject: Situation changes dramatically if Hibernate 3.0.5 is used
PostPosted: Tue Oct 31, 2006 1:45 am 
Newbie

Joined: Thu Feb 02, 2006 10:55 pm
Posts: 3
Hi,

I noticed that if I plug-in Hibernate 3.0.5 instead of 3.2.0 the 'session.get' method does no loger curse the object and it is possible to search for it before successfuly deleting it.

I'm not sure whether it is a change in behaviour by design or bug in 3.2.0.ga

Rgeards

Maciek


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