Hibernate Exception: a different object with the same identifier value was alrea
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.

