1 Reply Latest reply on Jul 5, 2007 6:55 PM by ilhami visne

    [Newbie]Can't persist 2 new objects, if they have the same p

    ilhami visne Newbie

      I'm creating 2 not-persisted objects and they have the same persisted object as instance variable. if i try to persist the new objects, i got a Duplicate entry violation. i know what it means, but i don't know what is wrong.

      can you please look at these small classes and point what is missing? Thank you.

      @Entity
      public class Gene implements java.io.Serializable {
      @Id @GeneratedValue private Integer id;
      
      private String name;
      
      @OneToMany()
      private List<CancerType> cancerTypes;
      public Gene() { }
      
       // Setters and Getters
      }


      Table CancerType is filled with some data after table creation!
      @Entity
      public class CancerType implements java.io.Serializable {
      @Id @GeneratedValue private Integer id;
      
      private String name;
      
      public CancerType(){ }
      
      // Setters and Getters
      }


      And the main:
      Session session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
      session.beginTransaction();
      
      Gene g1 = new Gene();
      g1.setName("name");
      Gene g2 = new Gene();
      g2.setName("name");
      
      List<CancerType> cts = BasicDAO.session.createQuery("from CancerType").list();
      
      g1.setCancerType(cts);
      g2.setCancerType(cts);
      
      session.save(g1);
      session.save(g2);
      
      session.getTransaction().commit();
      


      Output:
      Hibernate: select cancertype0_.id as id0_, cancertype0_.name as name0_ from CancerType cancertype0_
      Hibernate: insert into Gene (name) values (?)
      Hibernate: insert into Gene (name) values (?)
      Hibernate: insert into Gene_CancerType (Gene_id, cancerTypes_id) values (?, ?)
      Hibernate: insert into Gene_CancerType (Gene_id, cancerTypes_id) values (?, ?)
      00:01:39,109 WARN JDBCExceptionReporter:77 - SQL Error: 1062, SQLState: 23000
      00:01:39,109 ERROR JDBCExceptionReporter:78 - Duplicate entry '1' for key 1
      00:01:39,109 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
      org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
      Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
       at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
       at tvsas1.Main.a2(Main.java:58)
       at tvsas1.Main.main(Main.java:64)
      Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 1
       at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
       at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
       ... 9 more
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
       at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
       at tvsas1.Main.a2(Main.java:58)
       at tvsas1.Main.main(Main.java:64)
      Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 1
       at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
       at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
       ... 9 more