Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session
I saw that in my code (Spring + Hibernate web application) I was getting the following exception:
HibernateSystemException: a different object with the same identifier value was already associated with the session
I checked my code and realized that the problem was caused by the fact that I was getting the same object twice. I modified it to the simplest possible case:
customerDao.getById(customerId);
customerDao.getById(customerId);
The above code causes the mentioned exception, but I don't understand why it's wrong to get the same object twice from Hibernate. It seems like a normal use case. The same object could be retrieved i.e. by running two different queries. Why should I worry about whether one query will return the same objects as the second query or not? Can anybody explain or point me to some source of information on this topic?
P.S. The Hibernate second-level cache is disabled in the application.
UPDATE:
When I changed the contents of customerDao.getById(customerId) from:
Customer result = new Customer();
getHibernateTemplate().load(result, customerId);
return result;
to
return getHibernateTemplate().get(Customer.class, customerId);
the exception was no longer thrown. There are a lot of articles on the Internet on the differences of load and get but none that I found mentioned this case. Can anyone shed some light upon the topic?