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