I didnt used the hibernate, i just use EJB3 and JBoss, i get this Exception when i try to save beans with entityManager.persisten().
Finance newFinance = new Finance(this.entityManager.find(User.class, userName), ...);
Finance newFinance2 = new Finance(this.entityManager.find(User.class, userName), ...);
this.entityManager.persistence(newFinance2); // I GOT THE EXCEPTION HERE
the Finance and the User are entity beans, which Finance has a User property.
And i got the org.hibernate.NonUniqueObjectException a different object with the same identifier value was already associated with the session: [entities.User] when the second persistence() be called.
I search in Internet, found some solve, but they are all about Hibernate. So what should I do only with EJB3?
Your JPA might use Hibernate underneath.
As to my understanding, the response for your second call (User object) is taken from cache.
Nevertheless you are saving the same user information twice in Finance table. And you should have unique key constraints defined in Finance table (or in table mapping), which includes user information. Because of that, when you try to save the object second time, it throws that exception.
If you haev this problem, how can we handle it? For isntance, session.contains(object o) ? Overwriting equals method on our dto?