5 Replies Latest reply on Dec 21, 2007 10:10 AM by Arbi Sookazian

    EntityHome.persist fails with @GeneratedValue

    Doug Orr Newbie

      I am using a simple Seam-Gen generated application. I want to change the @Id field in the model class to be auto-generated. I changed the annotation to read as follows:

      @Id @GeneratedValue(strategy=GenerationType.AUTO)


      When I run the application the EntityHome.persist throws an exception. Here is the stack trace:
      avax.servlet.ServletException: Error calling action method of component with id optionalParent:save
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:152)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:63)
       at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:60)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.SeamFilter.doFilter(SeamFilter.java:84)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:96)
       at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:220)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       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.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
       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.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      2007-08-15 16:16:36,421 ERROR [org.jboss.seam.web.ExceptionFilter] exception root cause
      javax.faces.FacesException: Error calling action method of component with id optionalParent:save
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
       at javax.faces.component.UICommand.broadcast(UICommand.java:106)
       at org.ajax4jsf.framework.ajax.AjaxViewRoot.processEvents(AjaxViewRoot.java:180)
       at org.ajax4jsf.framework.ajax.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:158)
       at org.ajax4jsf.framework.ajax.AjaxViewRoot.processApplication(AjaxViewRoot.java:329)
       at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:343)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:137)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:63)
       at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:60)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
       at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
       at org.jboss.seam.web.SeamFilter.doFilter(SeamFilter.java:84)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:96)
       at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:220)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       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.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
       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.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      Caused by: javax.faces.el.EvaluationException: /OptionalParentEdit.xhtml @54,67 action="#{optionalParentHome.persist}": javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.genasys.testproject.optionalparent.OptionalParent
       at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73)
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
       ... 43 more
      Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.genasys.testproject.optionalparent.OptionalParent
       at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
       at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
       at org.jboss.seam.persistence.EntityManagerProxy.persist(EntityManagerProxy.java:128)
       at org.jboss.seam.framework.EntityHome.persist(EntityHome.java:48)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:20)
       at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:57)
       at org.jboss.seam.interceptors.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:37)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:34)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.TransactionInterceptor$1.work(TransactionInterceptor.java:32)
       at org.jboss.seam.util.Work.workInTransaction(Work.java:37)
       at org.jboss.seam.interceptors.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:27)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.interceptors.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:27)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:103)
       at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:151)
       at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:87)
       at com.genasys.testproject.optionalparent.OptionalParentHome_$$_javassist_122.persist(OptionalParentHome_$$_javassist_122.java)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at com.sun.el.parser.AstValue.invoke(AstValue.java:174)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:286)
       at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
       at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
       ... 44 more
      Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.genasys.testproject.optionalparent.OptionalParent
       at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
       at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
       at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
       at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
       at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
       at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
       ... 75 more
      


      Can anyone suggest the cause? What other changes do I need to make to support auto-generated ids?

        • 1. Re: EntityHome.persist fails with @GeneratedValue
          fhh Expert

          If you want to update an entity use merge (not persist).

          Regards

          Felix

          • 2. Re: EntityHome.persist fails with @GeneratedValue
            Doug Orr Newbie

            This was not an update process. This exception was raised when I tried to Create a new instance of an entity using the Seam-Gen generated pages. The only change I made to the application was to annotate the Id of the class to use @GeneratedValue(strategy=GenerationType.AUTO).

            • 3. Re: EntityHome.persist fails with @GeneratedValue
              Pete Muir Master

              The exception is clear. You are passing an entity Hibernate knows about (i.e. is already persisted) to persist. You can't do that. You'll need to use a debugger to find out why.

              • 4. Re: EntityHome.persist fails with @GeneratedValue
                Arbi Sookazian Master

                JBOSS4.2.1.GA
                SEAM2.0.0.GA

                Same problem here (but w/o EntityHome design pattern). I am seeing the following in the server.log before the exception. Occurs with or w/o using the TransactionalSeamPhaseListener in faces-config.xml. I am using SMPC with flushMode=MANUAL. This could be a programmer's error but I don't know why it states "mark transaction for rollback" when the persist() method is called on the EntityManager. There is no SQLException in the server.log.

                SFSB:

                for (int i = 0; i < 3; i++) {
                 TblSecurityAuditNote note = getTblSecurityAuditNote(currentRowNum, i);
                 if (note != null) {
                 log.info("myNotes["+currentRowNum+"]["+i+"]: noteId = " + note.getNoteId() + " noteText = " + note.getNoteText());
                 emICOMS.persist(note);
                 }
                 }
                
                 emICOMS.flush();


                stack trace:
                2007-12-20 15:16:59,074 INFO [com.cox.beans.interceptor.LoggerInterceptor] *** Entering method: submit
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 INFO [com.cox.beans.session.SecurityAuditAction] myNotes[0][1]: noteId = null noteText = asdf
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] executing identity-insert immediately
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.SQL] insert into boIcomsSecurityAudit.dbo.tblSecurityAuditNote (TimeStamp, NoteText, SITE_ID, EMPLOYEE_NUMBER, NoteType) values (?, ?, ?, ?, ?)
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.id.IdentifierGeneratorFactory] Natively generated identity: 262
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.jdbc.ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 INFO [com.cox.beans.session.SecurityAuditAction] myNotes[0][2]: noteId = 261 noteText = sdf
                2007-12-20 15:16:59,074 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] mark transaction for rollback
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [com.cox.beans.interceptor.ProfilingInterceptor] *** Method public void com.cox.beans.session.SecurityAuditAction.submit() executed in 0ms ***
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.util.Naming] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.interpolator
                2007-12-20 15:16:59,074 INFO [com.cox.beans.session.SecurityAuditAction] in preDestroy
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preRemoveVariable.securityAuditAction
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postRemoveVariable.securityAuditAction
                2007-12-20 15:16:59,074 DEBUG [org.jboss.seam.ejb.RemoveInterceptor] Stateful component was removed: securityAuditAction
                2007-12-20 15:16:59,090 ERROR [STDERR] Dec 20, 2007 3:16:59 PM javax.faces.event.MethodExpressionActionListener processAction
                SEVERE: Received 'javax.ejb.EJBTransactionRolledbackException' when invoking action listener '#{securityAuditAction.submit}' for component 'submitEmployee'
                2007-12-20 15:16:59,090 ERROR [STDERR] Dec 20, 2007 3:16:59 PM javax.faces.event.MethodExpressionActionListener processAction
                SEVERE: javax.ejb.EJBTransactionRolledbackException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.cox.beans.entity.TblSecurityAuditNote
                 at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)


                • 5. Re: EntityHome.persist fails with @GeneratedValue
                  Arbi Sookazian Master

                  well I determined the root cause of the exception I was getting (org.hibernate.PersistentObjectException: detached entity passed to persist: com.cox.beans.entity.TblSecurityAuditNote). It was that I had a loop that was trying to persist an already perstisted/inserted JPA entity into the DB and Hibernate notices this and throws the exception before you get a primary key violation SQLException from DB.

                  So in my case it was a programmer's error. The fix was to set the already persisted entity to null in the array so in the code it will not exec the em.persiste(note) again.

                  I think that the wording of the hibernate exception could be more clear. I still don't understand exactly what it's trying to indicate literally (I now know it's saying you're trying to insert a record in the table which has an existing primary key value).