-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Error trying to save objects with a List member
PostPosted: Thu Feb 22, 2007 7:40 pm 
Newbie

Joined: Thu Feb 22, 2007 7:05 pm
Posts: 2
Hi there,

I´m an Hibernate newbie, and I´m trying to load a database with many objects I had saved in an XML file. These objects include a field which is an ArrayList. I save the first one fine, but when I try to save the second one I get an exception. I´ve reproduced the problem with mock objects to simplify the posting.

Please help!

Hibernate version: 3.2

Mapping documents:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="persistence.Student" table="STUDENTS">
      <id name="id" column="STUDENT_ID">
         <generator class="native" />
      </id>

      <property name="name" type="string" column="STUDENT_NAME" />

      <list name="exams" table="EXAMS" cascade="save-update">
         <key column="STUDENT_ID" />
         <index column="EXAM_ID" />
         <one-to-many class="persistence.Exam" />
      </list>

   </class>
</hibernate-mapping>



Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>   
   <class name="persistence.Exam" table="EXAMS">
      <id name="id" column="EXAM_ID">
            <generator class="native"/>
        </id>
        <property name="subject" type="string" column="EXAM_SUBJECT"/>
        <property name="grade" type="string" column="EXAM_GRADE"/>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

The code that writes the objects is:

Code:
      sessionFactory = new Configuration().configure().buildSessionFactory();

      Student student = createStudent("John");
      saveStudent(student);

      student = createStudent("John");
      saveStudent(student);

      sessionFactory.close();



The method that actually writes:

Code:

   private void saveStudent(Student student) {
      Session session = sessionFactory.openSession();
      Transaction transaction = session.beginTransaction();
      session.saveOrUpdate(student);
      transaction.commit();
      session.close();
   }



And the objects:

Code:

public class Student {
   private Long id;
   private String name;
   private List<Exam> exams;

   public List<Exam> getExams() {
      return exams;
   }

   public void setExams(List<Exam> exams) {
      this.exams = exams;
   }

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
}

public class Exam {
   private Long id;
   private String subject;
   private String grade;

   public String getGrade() {
      return grade;
   }

   public void setGrade(String grade) {
      this.grade = grade;
   }

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getSubject() {
      return subject;
   }

   public void setSubject(String subject) {
      this.subject = subject;
   }
   
}



Full stack trace of any exception that occurs:

22-feb-2007 23:21:48 org.hibernate.util.JDBCExceptionReporter logExceptions
ADVERTENCIA: SQL Error: -104, SQLState: 23000
22-feb-2007 23:21:48 org.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: Violation of unique constraint $$: duplicate value(s) for column(s) $$: SYS_PK_221 in statement [update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?]
22-feb-2007 23:21:48 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
GRAVE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert collection: [persistence.Student.exams#2]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1183)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
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: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 persistence.HibernateTest.saveStudent(HibernateTest.java:63)
at persistence.HibernateTest.exec(HibernateTest.java:37)
at persistence.HibernateTest.main(HibernateTest.java:21)
Caused by: java.sql.SQLException: Violation of unique constraint $$: duplicate value(s) for column(s) $$: SYS_PK_221 in statement [update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1146)
... 12 more


Name and version of the database you are using: HSQL 1.8.0.7

The generated SQL (show_sql=true):

Hibernate: select max(STUDENT_ID) from STUDENTS
Hibernate: select max(EXAM_ID) from EXAMS
Hibernate: insert into STUDENTS (STUDENT_NAME, STUDENT_ID) values (?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
Hibernate: insert into STUDENTS (STUDENT_NAME, STUDENT_ID) values (?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?

Debug level Hibernate log excerpt:

22-feb-2007 23:21:41 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.1
22-feb-2007 23:21:41 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
22-feb-2007 23:21:41 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
22-feb-2007 23:21:41 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
22-feb-2007 23:21:41 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
22-feb-2007 23:21:41 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
22-feb-2007 23:21:41 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : Student.hbm.xml
22-feb-2007 23:21:42 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: persistence.Student -> STUDENTS
22-feb-2007 23:21:42 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : Exam.hbm.xml
22-feb-2007 23:21:42 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: persistence.Exam -> EXAMS
22-feb-2007 23:21:42 org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
22-feb-2007 23:21:42 org.hibernate.cfg.HbmBinder bindCollectionSecondPass
INFO: Mapping collection: persistence.Student.exams -> EXAMS
22-feb-2007 23:21:42 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
22-feb-2007 23:21:42 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 1
22-feb-2007 23:21:42 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
22-feb-2007 23:21:42 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost
22-feb-2007 23:21:42 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=sa, password=****}
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: HSQL Database Engine, version: 1.8.0
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: HSQL Database Engine Driver, version: 1.8.0
22-feb-2007 23:21:42 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.HSQLDialect
22-feb-2007 23:21:42 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
22-feb-2007 23:21:42 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
22-feb-2007 23:21:42 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
22-feb-2007 23:21:42 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
22-feb-2007 23:21:42 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
22-feb-2007 23:21:43 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
22-feb-2007 23:21:43 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: Running hbm2ddl schema export
22-feb-2007 23:21:43 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: exporting generated schema to database
22-feb-2007 23:21:43 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete
Hibernate: select max(STUDENT_ID) from STUDENTS
Hibernate: select max(EXAM_ID) from EXAMS
Hibernate: insert into STUDENTS (STUDENT_NAME, STUDENT_ID) values (?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
Hibernate: insert into STUDENTS (STUDENT_NAME, STUDENT_ID) values (?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
22-feb-2007 23:21:48 org.hibernate.util.JDBCExceptionReporter logExceptions
ADVERTENCIA: SQL Error: -104, SQLState: 23000
22-feb-2007 23:21:48 org.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: Violation of unique constraint $$: duplicate value(s) for column(s) $$: SYS_PK_221 in statement [update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?]
22-feb-2007 23:21:48 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
GRAVE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert collection: [persistence.Student.exams#2]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1183)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
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: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 persistence.HibernateTest.saveStudent(HibernateTest.java:63)
at persistence.HibernateTest.exec(HibernateTest.java:37)
at persistence.HibernateTest.main(HibernateTest.java:21)
Caused by: java.sql.SQLException: Violation of unique constraint $$: duplicate value(s) for column(s) $$: SYS_PK_221 in statement [update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1146)
... 12 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 11:57 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi lrmunoz,

Try generater class sequence for
<id name="id" column="EXAM_ID">
<generator class="native"/>
</id>

or check your logic for assigning Exam ID

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 3:51 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

you can't use the same column (EXAM_ID) as object-id and list-index....
The object-id has to be unique for all existing exam-objects, while the list-index will start at 0 for each student.
So what's happening?

Hibernate: select max(STUDENT_ID) from STUDENTS
Hibernate: select max(EXAM_ID) from EXAMS
==> should be null

Hibernate: insert into STUDENTS (STUDENT_NAME, STUDENT_ID) values (?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
==> inserts EXAM_ID = 1 (object-id)

Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
==> inserts EXAM_ID = 2 (object-id)

Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
==> sets EXAM_ID = 0 (first element in List) for EXAM_ID = 1

Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
==> sets EXAM_ID = 1 (second element in List) for EXAM_ID = 2

Hibernate: insert into STUDENTS (STUDENT_NAME, STUDENT_ID) values (?, ?)
Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
==> inserts EXAM_ID = 3 (object-id, hibernates internal counter says 3 is the next number to use)

Hibernate: insert into EXAMS (EXAM_SUBJECT, EXAM_GRADE, EXAM_ID) values (?, ?, ?)
==> inserts EXAM_ID = 4 (object-id)

Hibernate: update EXAMS set STUDENT_ID=?, EXAM_ID=? where EXAM_ID=?
==> sets EXAM_ID = 0 (first element in List) for EXAM_ID = 3 ==> crash!!!!

Greets

piet


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 6:35 am 
Newbie

Joined: Thu Feb 22, 2007 7:05 pm
Posts: 2
Thanks a lot Piet, it works!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.