3 Replies Latest reply on Aug 3, 2006 9:30 AM by hdslkf hklsdg

    How to catch exception when deleting?

    hdslkf hklsdg Apprentice

      I have a Stateful Session Bean with a method delete(). Guess what is does? It deletes an entity ;).

      @Stateful
      @Scope(Scopetype.SESSION)
      @name("foo")
      ...
      
       @In(create=true)
       private EntityManager em;
      
       public String delete() {
      
       em.remove(anEntity);
      
       return "finished";
      
       }
      


      Ok, now it could be that the user wants to delete an entity that is referenced by other entities. So an SqlException is thrown telling that a database constraint is violated. I wanted to catch that exception and tell the user that deletion wasn't successful. I tried it like this:

       public String delete() {
      
       try {
       em.remove(anEntitiy);
       } catch (Exception ex) {
       ...addMessage("Deletion failed!");
       }
       return "finished";
       }
      


      Unfortunately that doesn't work, the exception isn't caught and Facelets displays an ugly error message.

      Well... why? *g*


      Here's the log. It's long but please don't be shocked ;).

      2006-08-02 18:25:38,359 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
      2006-08-02 18:25:38,359 DEBUG [org.hibernate.SQL] delete from Person where id=? and version=?
      2006-08-02 18:25:38,359 INFO [STDOUT] Hibernate: delete from Person where id=? and version=?
      2006-08-02 18:25:38,375 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
      2006-08-02 18:25:38,375 DEBUG [org.hibernate.jdbc.ConnectionManager] skipping aggressive-release due to flush cycle
      2006-08-02 18:25:38,375 DEBUG [org.hibernate.util.JDBCExceptionReporter] could not delete: [de.beffo.test.model.Person#9] [delete from Person where id=? and version=?]
      java.sql.SQLException: Cannot delete or update a parent row: a foreign key constraint fails (`test/person_person`, CONSTRAINT `FKB9039F7F77F35DCE` FOREIGN KEY (`freunde_id`) REFERENCES `person` (`id`))
       at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
       at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
       at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1124)
       at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:676)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
       at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2366)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2516)
       at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
       at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:340)
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:475)
       at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
       at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
       at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
       at org.jboss.tm.TxManager.commit(TxManager.java:240)
       at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:188)
       at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
       at $Proxy112.loeschen(Unknown Source)
       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:151)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
       at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
       at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
       at javax.faces.component.UICommand.broadcast(UICommand.java:106)
       at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
       at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:132)
       at org.apache.myfaces.lifecycle.LifecycleImpl.applyRequestValues(LifecycleImpl.java:200)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:71)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:30)
       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.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.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)
      2006-08-02 18:25:38,375 WARN [org.hibernate.util.JDBCExceptionReporter] SQL Error: 1451, SQLState: 23000
      2006-08-02 18:25:38,375 ERROR [org.hibernate.util.JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`test/person_person`, CONSTRAINT `FKB9039F7F77F35DCE` FOREIGN KEY (`freunde_id`) REFERENCES `person` (`id`))
      2006-08-02 18:25:38,375 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.ConstraintViolationException: could not delete: [de.beffo.test.model.Person#9]
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2384)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2516)
       at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
       at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:340)
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:475)
       at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
       at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
       at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
       at org.jboss.tm.TxManager.commit(TxManager.java:240)
       at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:188)
       at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
       at $Proxy112.loeschen(Unknown Source)
       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:151)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
       at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
       at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
       at javax.faces.component.UICommand.broadcast(UICommand.java:106)
       at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
       at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:132)
       at org.apache.myfaces.lifecycle.LifecycleImpl.applyRequestValues(LifecycleImpl.java:200)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:71)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:30)
       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.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.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: java.sql.SQLException: Cannot delete or update a parent row: a foreign key constraint fails (`test/person_person`, CONSTRAINT `FKB9039F7F77F35DCE` FOREIGN KEY (`freunde_id`) REFERENCES `person` (`id`))
       at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
       at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
       at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1124)
       at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:676)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
       at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2366)
       ... 67 more
      2006-08-02 18:25:38,375 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection
      2006-08-02 18:25:38,375 DEBUG [org.hibernate.jdbc.ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
      2006-08-02 18:25:38,421 DEBUG [org.hibernate.jdbc.JDBCContext] TransactionFactory reported no active transaction; Synchronization not registered
      2006-08-02 18:25:38,421 DEBUG [org.hibernate.jdbc.JDBCContext] TransactionFactory reported no active transaction; Synchronization not registered
      2006-08-02 18:25:38,421 DEBUG [org.hibernate.jdbc.JDBCContext] TransactionFactory reported no active transaction; Synchronization not registered
      2006-08-02 18:25:38,421 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.parentTraceEnabled=true
      2006-08-02 18:25:38,421 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.nestedTraceEnabled=false
      2006-08-02 18:25:38,421 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.detectDuplicateNesting=true
      2006-08-02 18:25:38,437 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/test].[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
      javax.faces.FacesException: Error calling action method of component with id _id19:_id42
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
       at javax.faces.component.UICommand.broadcast(UICommand.java:106)
       at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
       at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:132)
       at org.apache.myfaces.lifecycle.LifecycleImpl.applyRequestValues(LifecycleImpl.java:200)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:71)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:30)
       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.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.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: /personEditor.xhtml @82,124 action="#{personEditor.loeschen}": java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=SCHLEPPI/34, BranchQual=, localId=34] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete: [de.beffo.test.model.Person#9])
       at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73)
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
       ... 27 more
      Caused by: java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=SCHLEPPI/34, BranchQual=, localId=34] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete: [de.beffo.test.model.Person#9])
       at org.jboss.aspects.tx.TxPolicy.handleEndTransactionException(TxPolicy.java:198)
       at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:180)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:188)
       at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
       at $Proxy112.loeschen(Unknown Source)
       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:151)
       at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
       at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
       at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
       ... 28 more
      Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=SCHLEPPI/34, BranchQual=, localId=34] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete: [de.beffo.test.model.Person#9])
       at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:372)
       at org.jboss.tm.TxManager.commit(TxManager.java:240)
       at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
       ... 52 more
      Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete: [de.beffo.test.model.Person#9]
       at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:555)
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:484)
       at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
       at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
       at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
       ... 54 more
      Caused by: org.hibernate.exception.ConstraintViolationException: could not delete: [de.beffo.test.model.Person#9]
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2384)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2516)
       at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
       at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:340)
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:475)
       ... 57 more
      Caused by: java.sql.SQLException: Cannot delete or update a parent row: a foreign key constraint fails (`test/person_person`, CONSTRAINT `FKB9039F7F77F35DCE` FOREIGN KEY (`freunde_id`) REFERENCES `person` (`id`))
       at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
       at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
       at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1124)
       at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:676)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
       at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
       at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
       at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2366)
       ... 67 more
      
      


        • 1. Re: How to catch exception when deleting?
          hdslkf hklsdg Apprentice

          Well, I just had a look over the stacktrace again and something jumped into my eye:

          at $Proxy112.loeschen(Unknown Source)
          
          or in english:
          
          at $Proxy112.delete(Unknown Source)
          


          It seems like the container replaces the delete method by some kind of proxy that doesn't catch exceptions. I don't know exactly what's going on behind the scenes here, maybe someone of you can give me a nice hint.

          • 2. Re: How to catch exception when deleting?
            Gavin King Master

            You would need to do:

            public String delete() {
            
             try {
             em.remove(anEntitiy);
             em.flush();
             } catch (Exception ex) {
             ctx.setRollbackOnly();
             ...addMessage("Deletion failed!");
             }
             return "finished";
             }




            • 3. Re: How to catch exception when deleting?
              hdslkf hklsdg Apprentice

              Thanks for that hint, Gavin. It works now (*) and I learned something (once more after one of your posts) ;).

              Btw, for those who have the same problem and read this: ctx is the EJB Session Context, and you can get it via

              @Resource
              private SessionContext ctx;


              Don't mistake that for the Seam sessionContext, which can be retrieved by @In private Context sessionContext ;).



              (*) Little anecdote: I logged the exception, and since it contained a # (could not delete: [de.beffo.test.model.Person#9]) the JBoss Seam logger tried to perform a replacement here... and *boom* there was an exception and I was near desperation. But after a second glance I found the reason, deleted that line and finally everything worked fine ;). But I even had to remove that dash from addMessage(...) cause otherwise there's an internal similar exception that causes the message not to be created. *Uff*... J2EE is hard work ;).