• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 HibernateException on EntityManager.remove(object) function  RSS feed

Scafuro Te
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I have a problem on my ejb application...

On runing the project, everytime i try to do a delete of a specifical record, i have this exception:

javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [ejb.Profili#26428513]

I found that the problem is dued to this part of code:

HttpSession session = request.getSession(true);
ProfiliFacadeLocal profiliFacade = (ProfiliFacadeLocal) lookupProfiliFacade();
List profili = profiliFacade.findAll();
String key =(String)session.getAttribute("idProf");

for (Iterator itProf = profili.iterator(); itProf.hasNext(); ) {
Profili elem = (Profili) itProf.next();
out.println("<h2>Rimozione esame : " +elem.getIdcorso()+ " riuscita</h2");


where the profiliFacade.destroy(profili) function is so defined:

public void destroy(Profili profili) {
//with em as "EntityManager em"

All the entitymanager operation like merge, create and so one give me no problem, it works, i have the problem only with the
does someone know where is the bug and what i have to do?
Mark Spritzler
Posts: 17309
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, you merge, but you don't merge.

This is what I mean, when you call merge you pass an Object in. That Object is not going to point to the merged object after the call to merge. merge() returns a merged object. So your profili reference is still pointing to the unmerged object, and when you pass it to delete() you get your error

you can try

profili = em.merge(profili);

and now your delete line after that will work.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!