Hibernate Exception: a different object with the same identifier value was alrea

Hi all,
I'm working on a Java application using Hibernate3 as persistence model. I'm quite new in Java, and I never worked with Hibernate before.

I'm working in a java project (called Core), which it'll be called from a JSP (struts) or from a webservice. To test this project (I haven't begun JSP nor Webservices), I've made another Java Project to test the methods, that will be called from JSP and WS. I also use JUnit to test it.

Before main I have an static block to initialize hibernate in this way:
configuration = new Configuration();
sessionFactory = configuration.configure("hibernate.cfg.xml").buildSessionFactory();
I start a every session using:
session = getSessionFactory().getCurrentSession().beginTransaction();

My problem is next:
Inside a function within the same session, I get an object from DB doing:
personBD = (PersonVO) getCurrentSession().get(PersonaVO.class, new Long( personaVO.getIdPersonaCdm()));
Then I create another instance of PersonVO (which is a Java Bean), and I try to update this new object in this way:
getCurrentSession().update(person);

This throws and Hibernate exception: "a different object with the same identifier value was already associated with the session"

Looking for help, I read that I cannot update another object because Hibernate puts the object retrieved in a Cache, and it doesn't allow to update using another instance (of the same class, of course).

I found a function called evict(), to free an object from the cache, and I use it in this way:
personBD = (PersonVO) getCurrentSession().get(PersonaVO.class, new Long( personaVO.getIdPersonaCdm())); //Retrieve the object
... //business logic
getCurrentSession().evict(personBD);
getCurrentSession().update(person);
Now, it throws another exception, but it's not thrown doing update, but when I try to do a commit.
getCurrentSession().getTransaction().commit();

The exception message is: "possible nonthreadsafe access to session"
The whole message stored in log file: "11:20:09,442 ERROR assertionFailure:22 - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) org.hibernate.AssertionFailure: possible nonthreadsafe access to session"

I think I have a problem with my session, but I don't know how to find it. Am I doing anything wrong?

If you need any other information about what I'm doing, just ask. I guess it's not important, but I'm working under XP SP2, using WebSphere 5.1.2, Hibernate3 and IBM Java SDK.

Thanks a lot, and sorry for my english.
[2748 byte] By [AlbertGM] at [2007-11-20 11:56:33]
# 1 Re: Hibernate Exception: a different object with the same identifier value was alrea
Hi again,

I have fixed the problem. The problem was in other part of code doing:

Iterator it = historicoCambiosVO.iterator();
while(it.hasNext()){
HistoricoCambioVO elementoHistoricoCambioVO = (HistoricoCambioVO) it.next();

elementoHistoricoCambioVO.setIdMovimiento(movimiento);

HibernateUtil.getCurrentSession().save(elementoHistoricoCambioVO);
}
I think the error was try to save the 'same object' (for Hibernate) which was a diferent object (for Java). The solution that worked to me is:

Iterator it = historicoCambiosVO.iterator();
while(it.hasNext()){
HistoricoCambioVO elementoHistoricoCambioVO = (HistoricoCambioVO) it.next();

elementoHistoricoCambioVO.setIdMovimiento(movimiento);

HibernateUtil.getCurrentSession().save(elementoHistoricoCambioVO);
HibernateUtil.getCurrentSession().flush();
HibernateUtil.getCurrentSession().evict(elementoHistoricoCambioVO);
}
I'm not sure why this works, and why it didn't work before. I'll thank any explanation.

Albert.
AlbertGM at 2007-11-10 2:13:57 >
# 2 Re: Hibernate Exception: a different object with the same identifier value was alrea
I think you'll have better success on the Hibernate forums (http://forum.hibernate.org/), but please read this (http://www.hibernate.org/160.html) first.

It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
F. Gauss
dlorde at 2007-11-10 2:15:00 >