0 Replies Latest reply on Aug 23, 2005 8:01 PM by Darko Krizic

    Problem when persisting bean with reference

    Darko Krizic Newbie

      Hi!

      I have this exception (full trace on end) when doing a persist:

      java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dkrizict42p/35, BranchQual=, localId=35] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)
      Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dkrizict42p/35, BranchQual=, localId=35] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)
      Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      Caused by: java.sql.BatchUpdateException: Duplicate entry '1000' for key 1
      


      The client does basically this (from a test case):

       UserProcess um = getUserManagement();
      
       Branch b = um.findBranchById( b1 );
       User u = new User(u1, "dkrizic", "Darko", "Krizic", "abc123", b );
       um.addUser( u );
      


      This is the relevant part of the User entity bean

      @Entity
      @EntityListener(com.prodyna.soa.backend.management.GenericEntityLogger.class)
      @Table(name = "xuser")
      public class User implements Serializable, Displayable {
      
       private Branch branch;
      
       @Id
       @Column(name = "user_id")
       public Long getId() {
       return id;
       }
      
       public void setId(Long id) {
       this.id = id;
       }
      
       @OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
       public Set<Session> getSessions() {
       return sessions;
       }
      
       public void setSessions(Set<Session> sessions) {
       this.sessions = sessions;
       }
      
       @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
       @JoinColumn(name = "branch_id_ref", nullable = true)
       public Branch getBranch() {
       return branch;
       }
      
       public void setBranch(Branch branch) {
       this.branch = branch;
       }
      
       @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
       CascadeType.MERGE })
       @JoinTable(table = @Table(name = "xuser_xgroup"), joinColumns = { @JoinColumn(name = "user_id_ref", referencedColumnName = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "group_id_ref", referencedColumnName = "group_id") })
       public Set<Group> getGroups() {
       return groups;
       }
      
       public void setGroups(Set<Group> groups) {
       this.groups = groups;
       }
      }
      


      This is what the log says (GenericSessionLogger):

      01:43:42,323 INFO [UserProcessBean] findBranchById(1000) [admin]
      01:43:42,383 INFO [UserProcessBean] addUser(id=[100] name=[dkrizic] password=[abc123] locked=[false] unsuccessfulTries=[0] version=[0] branch=[id=[1000] name=[Opernplatz] street=[Am Opernplatz] houseNumber=[35] zip=[60123] city=[Frankfurt] country=[Deutschland]]) [admin]
      01:43:42,733 WARN [JDBCExceptionReporter] SQL Error: 1062, SQLState: 23000
      01:43:42,733 ERROR [JDBCExceptionReporter] Duplicate entry '1000' for key 1
      


      This is what mysql trace says

       3 Prepare [28] insert into branch (name, country, city
      , house_number, street, zip, branch_id) values (?, ?, ?, ?, ?, ?, ?)
       3 Execute [28] insert into branch (name, country, city
      , house_number, street, zip, branch_id) values (?, ?, ?, ?, ?, ?, ?)
       3 Query rollback
      050824 1:43:43 3 Prepare [29] select user0_.user_id as user1_5_3_, us
      er0_.name as name5_3_, user0_.is_locked as is8_5_3_, user0_.password as password
      5_3_, user0_.unsuccessfull_tries as unsucce10_5_3_, user0_.branch_id_ref as bran
      ch14_5_3_, user0_.first_name as first11_5_3_, user0_.last_name as last12_5_3_, s
      essions1_.user_id_ref as user13_5_, sessions1_.session_id as session1_5_, sessio
      ns1_.session_id as session1_5_0_, sessions1_.is_valid as is2_5_0_, sessions1_.us
      er_id_ref as user13_5_0_, sessions1_.created as created5_0_, sessions1_.last_acc
      ess as last4_5_0_, sessions1_.token as token5_0_, groups2_.user_id_ref as user1_
      6_, group3_.group_id as group2_6_, group3_.group_id as group1_3_1_, group3_.name
       as name3_1_, group3_.description as descript3_3_1_, branch4_.branch_id as branc
      h1_1_2_, branch4_.name as name1_2_, branch4_.country as country1_2_, branch4_.ci
      ty as city1_2_, branch4_.house_number as house5_1_2_, branch4_.street as street1
      _2_, branch4_.zip as zip1_2_ from xuser user0_ left outer join xuser sessions1_
      on user0_.user_id=sessions1_.user_id_ref left outer join xuser_xgroup groups2_ o
      n user0_.user_id=groups2_.user_id_ref left outer join xgroup group3_ on groups2_
      .group_id_ref=group3_.group_id left outer join branch branch4_ on user0_.branch_
      id_ref=branch4_.branch_id where user0_.user_id=?
       3 Execute [29] select user0_.user_id as user1_5_3_, us
      er0_.name as name5_3_, user0_.is_locked as is8_5_3_, user0_.password as password
      5_3_, user0_.unsuccessfull_tries as unsucce10_5_3_, user0_.branch_id_ref as bran
      ch14_5_3_, user0_.first_name as first11_5_3_, user0_.last_name as last12_5_3_, s
      essions1_.user_id_ref as user13_5_, sessions1_.session_id as session1_5_, sessio
      ns1_.session_id as session1_5_0_, sessions1_.is_valid as is2_5_0_, sessions1_.us
      er_id_ref as user13_5_0_, sessions1_.created as created5_0_, sessions1_.last_acc
      ess as last4_5_0_, sessions1_.token as token5_0_, groups2_.user_id_ref as user1_
      6_, group3_.group_id as group2_6_, group3_.group_id as group1_3_1_, group3_.name
       as name3_1_, group3_.description as descript3_3_1_, branch4_.branch_id as branc
      h1_1_2_, branch4_.name as name1_2_, branch4_.country as country1_2_, branch4_.ci
      ty as city1_2_, branch4_.house_number as house5_1_2_, branch4_.street as street1
      _2_, branch4_.zip as zip1_2_ from xuser user0_ left outer join xuser sessions1_
      on user0_.user_id=sessions1_.user_id_ref left outer join xuser_xgroup groups2_ o
      n user0_.user_id=groups2_.user_id_ref left outer join xgroup group3_ on groups2_
      .group_id_ref=group3_.group_id left outer join branch branch4_ on user0_.branch_
      id_ref=branch4_.branch_id where user0_.user_id=?
       3 Query rollback
      


      Server side is (shortened)

      @Stateless
      @Interceptor(value = com.prodyna.soa.backend.management.GenericSessionLogger.class)
      @SecurityDomain("banking")
      public class UserProcessBean implements UserProcess, Serializable {
      
       private static final long serialVersionUID = 3256439218279627060L;
      
       private @PersistenceContext
       EntityManager em;
      
       public User addUser(User user) {
       em.persist(user);
       return user;
       }
      
       public Branch findBranchById(long id) {
       Branch b = em.find(Branch.class, id);
       if( b == null ) {
       throw new EntityNotFoundException( Branch.class.getName() + " " + id );
       }
       return b;
       }
      }
      



      Environment: JBoss 4.0.3RC2 and EJB 3.0RC1

      this code worked on an older version, I am very confused

      Full trace


      java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dkrizict42p/35, BranchQual=, localId=35] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.exception.ConstraintViolationException: 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:88)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      at org.jboss.aspects.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:152)
      at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:78)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:195)
      at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
      at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:69)
      at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:566)
      at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:436)
      at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:239)
      at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:291)
      at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:168)
      Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dkrizict42p/35, BranchQual=, localId=35] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.exception.ConstraintViolationException: 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)
      ... 22 more
      Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:182)
      at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:75)
      at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:70)
      at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:151)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1910)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2287)
      at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:48)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:284)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:750)
      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)
      ... 23 more
      Caused by: java.sql.BatchUpdateException: Duplicate entry '1000' for key 1
      at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:822)
      at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:484)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:175)
      ... 40 more