EJB programming & troubleshooting: org.hibernate.NonUniqueObjectException

  1. org.hibernate.NonUniqueObjectException (2 messages)

    Hello, 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), ...); this.entityManager.persistence(newFinance); 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? thanks!
  2. Hi, 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.
  3. how to avoid it?[ Go to top ]

    If you haev this problem, how can we handle it? For isntance, session.contains(object o) ? Overwriting equals method on our dto?