-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: EntityNotFoundException Exception - Saving a removed entity
PostPosted: Mon Jul 02, 2007 3:34 pm 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
Hi there!

Im developing a backend with an Applet as a frontend. And i am with a problem persisting a entity.

When an entity is created (new operator, that create a entity with out id, and properties are setted) on the client side, it is passed to to the servlet where it is persisted with the following code:
Code:
ReturnObject returnObject = new ReturnObject();
EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
try{
     transaction.begin();
     //check if mandatory field are filled
     returnObject.addReturnObject(checkImovelFields(imovel));
           
     if(returnObject.hasErrors()){
        transaction.rollback();
     } else {   
        em.merge(imovel);
        returnObject.addInfoMessage(IConstants.MSG_INFO_SUCESS_ON_CREATION);
         transaction.commit();
    }
} catch(Exception e){
    e.printStackTrace();
    String exceptionCause = getExceptionCause(e);
           
    returnObject.addErrorMessage(exceptionCause);
    transaction.rollback();
} finally {
     em.close();
     return returnObject;
}


When a user edit a entity, to control concorrent acess, on the servlet on update time, i compare the initial state ot the entity on the client side with the entity on the DB side to check if the entity was change by another user. If so, the user get a message saying that the entity was changed, and if he want to override the information. Till here everything runs allrigth.

My problem is if im editing an entity and, the entity is removed by another user. The remove code is following:

Code:
ReturnObject returnObject = new ReturnObject();
EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
       
try{
    transaction.begin();
    ImovelHabitacional imovelToRemove = em.find(ImovelHabitacional.class, imovelHabitacionalId);
    em.remove(imovelToRemove);
    transaction.commit();
} catch(Exception e){
    transaction.rollback();
           
    e.printStackTrace();
    String exceptionCause = getExceptionCause(e);
    returnObject.addErrorMessage(exceptionCause);
           
} finally {
    em.close();
    return returnObject;
}


The servlet detects that that entity no longer exists on the DB, and so the user get's a message saying that entity no longer exist, and if he want to save any way that entity. If the answer is "YES" then on the client side i create a new instance of the entity just like the normal creation, that works fine, copy the properties values to this new entity (to simulate a creation) and send it to the servlet to the creation method.

Some how, the entity manager still thinks that entity still exists and raise the follow exception: (the ImovelHabitacional is the entity that i am dealing with)

Full stack trace of any exception that occurs:
Code:
avax.persistence.EntityNotFoundException: Unable to find immo.persistence.ImovelHabitacional with id 1
        at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java :107)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:145)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
        at org.hibernate.impl.SessionImpl.internalLoad (SessionImpl.java:846)
        at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
        at org.hibernate.type.EntityType.resolve(EntityType.java:379)
        at org.hibernate.type.EntityType.replace (EntityType.java:259)
        at org.hibernate.type.AbstractType.replace(AbstractType.java:153)
        at org.hibernate.type.TypeFactory.replace(TypeFactory.java:495)
        at org.hibernate.event.def.DefaultMergeEventListener.copyValues (DefaultMergeEventListener.java:377)
        at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:179)
        at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached (DefaultMergeEventListener.java:240)
        at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
        at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
        at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
        at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
        at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java :268)
        at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
        at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
        at org.hibernate.engine.Cascade.cascadeCollectionElements (Cascade.java:296)
        at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
        at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
        at org.hibernate.engine.Cascade.cascadeProperty (Cascade.java:169)
        at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
        at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
        at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient (DefaultMergeEventListener.java:194)
        at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
        at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java :53)
        at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
        at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
        at org.hibernate.impl.SessionImpl.merge(SessionImpl.java :665)
        at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:227)
        at immo.servlets.ImovelHabitacionalServlet.createImovelHabitacional(ImovelHabitacionalServlet.java:161)
        at immo.servlets.ImovelHabitacionalServlet.doPost(ImovelHabitacionalServlet.java:81)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        at javax.servlet.http.HttpServlet.service( HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java :173)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.coyote.http11.Http11Processor.process (Http11Processor.java:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket (PoolTcpEndpoint.java:527)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java :684)
        at java.lang.Thread.run(Thread.java:619)



Any one know what am i doing wrong or what am i forgeting to do?

thanx a lot

Hibernate version:
Hibernate Core 3.2.2 GA
Hibernate EntityManager 3.2.1 GA

Name and version of the database you are using:
Mysql 5


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 6:57 pm 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
I already fixed, but i dont understand why! Follow is code:

Code:
ImovelHabitacional newImovelHabitacional = new ImovelHabitacional();
newImovelHabitacional.copyImovelHabitacional(imovelHabitacional);
returnObject = ImovelHabitacionalServletRequestsFacade.saveImovelHabitacional(newImovelHabitacional);

I thought that i should create a new instance of ImovelHabitacional, but with the information of the entity to be persisted, so i created a new instance and copy the values to it.

The i tryed to just put the id of the entity with the null value a "create" it like it was the first time. and it worked fine.
Code:
//ImovelHabitacional newImovelHabitacional = new ImovelHabitacional();
//newImovelHabitacional.copyImovelHabitacional(imovelHabitacional);
imovelHabitacional.setId(null);
returnObject = ImovelHabitacionalServletRequestsFacade.saveImovelHabitacional(imovelHabitacional);


To the EntityManager what is the diference between doing em.merge(newImovelHabitacional) and em.merge(imovelHabitacional). On the first case, why should the EntityManager thought it was the entity that was removed?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.