• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 HibernateException on EntityManager.remove(object) function

 
Scafuro Te
Greenhorn
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);
try{
ProfiliFacadeLocal profiliFacade = (ProfiliFacadeLocal) lookupProfiliFacade();
List profili = profiliFacade.findAll();
String key =(String)session.getAttribute("idProf");
try{

for (Iterator itProf = profili.iterator(); itProf.hasNext(); ) {
Profili elem = (Profili) itProf.next();
if(elem.getIdprof().equals(request.getParameter("idProf"))){
out.println("<h2>Rimozione esame : " +elem.getIdcorso()+ " riuscita</h2");
profiliFacade.destroy(elem);

}
}


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

public void destroy(Profili profili) {
em.merge(profili);
em.remove(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
EntityManager.remove(object)
function:
does someone know where is the bug and what i have to do?
tkx
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
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.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic