0 Replies Latest reply on Mar 17, 2006 12:02 PM by Krzysztof Bankiewicz

    Problem with deleting object from ManyToMany relationship.

    Krzysztof Bankiewicz Newbie

      Hi,

      I've a problem with deleting object from ManyToMany relationship without deleting objects connected with it. In my project there are two classes: User and Role connected by ManyToMany relationship. Below there is a code:

      package project.nncs3.par;
      
      import java.io.Serializable;
      import java.util.Collection;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratorType;
      import javax.persistence.Id;
      import javax.persistence.ManyToMany;
      import javax.persistence.Table;
      
      import project.nncs3.web.UserBean;
      
      @Entity
      @Table(name = "extents_user")
      public class User implements Serializable {
       static final long serialVersionUID = 0;
      
       private int idPK;
      
       private String login;
       private String password;
      // private Person person;
       private Collection<Role> roles;
      
       public User() {
       }
      
       public User(int idPK, String login, String password) {
       this.idPK = idPK;
       this.login = login;
       this.password = password;
       }
      
       public User(UserBean user) {
       this.idPK = user.getIdPK();
       this.login = user.getLogin();
       this.password = user.getPassword();
       this.roles = user.getRoles();
       }
      
       @Id (generate = GeneratorType.AUTO)
       @Column (name = "id_PK")
       public int getIdPK() { return idPK; }
       public void setIdPK(int idPK){ this.idPK = idPK; }
      
      /* @OneToOne
       @JoinColumn(name = "personIdPK_FK")
       public Person getPerson() { return person; }
       public void setPerson(Person person) { this.person = person; }*/
      
       @ManyToMany(mappedBy="users", fetch = FetchType.EAGER)
       public Collection<Role> getRoles() { return roles; }
       public void setRoles(Collection<Role> roles){ this.roles = roles; }
      
       public String getLogin() { return login; }
       public void setLogin(String login) { this.login = login; }
      
       public String getPassword() { return password; }
       public void setPassword(String password) { this.password = password; }
      }
      
      package project.nncs3.par;
      
      import java.io.Serializable;
      import java.util.Collection;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratorType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToMany;
      import javax.persistence.Table;
      
      @Entity
      @Table(name = "extents_role")
      public class Role implements Serializable {
       static final long serialVersionUID = 0;
       private int idPK;
       private String name;
       private Collection<User> users;
      
       public Role() {
       }
      
       @Id (generate = GeneratorType.AUTO)
       @Column (name = "id_PK")
       public int getIdPK() { return idPK; }
       public void setIdPK(int idPK){ this.idPK = idPK; }
      
       @ManyToMany(fetch = FetchType.EAGER)
       @JoinTable(
       table=@Table(name="associations_role_user"),
       joinColumns={@JoinColumn(name="roleIdPK_FK")},
       inverseJoinColumns={@JoinColumn(name="userIdPK_FK")}
       )
       public Collection<User> getUsers() { return users; }
       public void setUsers(Collection<User> users){ this.users = users; }
       public void setName(String name){ this.name = name; }
       public String getName(){ return name; }
      }
      
      


      When I try to remove only user, without removing roles connected with it, just using EntityManager remove(user) method.

      I get this Exception:

      [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:200)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:905)
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:185)
      at org.jboss.ejb3.entity.ManagedEntityManagerFactory$SessionSynchronization.beforeCompletion(ManagedEntityManagerFactory.java:89)
      at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1473)
      at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1092)
      at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:306)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:162)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:74)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:189)
      at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:37)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:88)
      at $Proxy113.removeUser(Unknown Source)
      at project.nncs3.web.UserRow.remove(UserRow.java:58)
      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.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
      at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
      at javax.faces.component.UICommand.broadcast(UICommand.java:106)
      at javax.faces.component.UIData.broadcast(UIData.java:338)
      at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
      at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:271)
      at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:94)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
      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.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
      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:856)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
      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.BatchUpdateException: failed batch
      at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
      at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
      at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:487)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
      ... 64 more
      17:39:10,531 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
      javax.faces.FacesException: Error calling action method of component with id allUsersView:_id13_1:_id20
      at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
      at javax.faces.component.UICommand.broadcast(UICommand.java:106)
      at javax.faces.component.UIData.broadcast(UIData.java:338)
      at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
      at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:271)
      at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:94)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
      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.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
      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:856)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
      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: Exception while invoking expression #{tmpUser.remove}
      at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:153)
      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=czarniecki/87, BranchQual=, localId=87] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)
      at org.jboss.aspects.tx.TxPolicy.handleEndTransactionException(TxPolicy.java:185)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:167)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:74)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:189)
      at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:37)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:88)
      at $Proxy113.removeUser(Unknown Source)
      at project.nncs3.web.UserRow.remove(UserRow.java:58)
      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.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
      ... 28 more
      Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=czarniecki/87, BranchQual=, localId=87] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)
      at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:354)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:162)
      ... 53 more
      Caused by: org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:200)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:905)
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:185)
      at org.jboss.ejb3.entity.ManagedEntityManagerFactory$SessionSynchronization.beforeCompletion(ManagedEntityManagerFactory.java:89)
      at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1473)
      at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1092)
      at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:306)
      ... 54 more
      Caused by: java.sql.BatchUpdateException: failed batch
      at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
      at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
      at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:487)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
      ... 64 more


      And even when I remove references connecting user with role in my removeUser() method:
       public void removeUser(User user) {
       for (Role role : user.getRoles()) {
       role.getUsers().remove(user);
       em.merge(role);
       }
       user.getRoles().clear();
       em.merge(user);
       em.remove(user);
       }
      
      

      I get exception like this:

      Caused by: javax.ejb.EJBException: null; CausedByException is:
      a different object with the same identifier value was already associated with the session: [project.nncs3.par.User#2]
      at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:189)
      at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:37)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:88)
      at $Proxy84.removeUser(Unknown Source)
      at project.nncs3.web.UserRow.remove(UserRow.java:58)
      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.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
      ... 28 more


      Does anyone meet with the same problem and solve it?

      I use JBoss 4.0.3 SP1 and HSQL Database.