Divan wrote:
I have code that creates a CSVFile object, adds 3 records to it and save it, then ad another CSVFile and add two records and save it, but when I call the commit() methot on the transaction I get the following stacktrace
net.sf.hibernate.JDBCException: could not insert collection: [biz.c24.io.csv.CSVFile.record#2]
at net.sf.hibernate.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:543)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at biz.c24.io.csv.Main.createTestData(Main.java:102)
at biz.c24.io.csv.Main.main(Main.java:136)
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:324)
at com.intellij.rt.execution.application.AppMain.main(Unknown Source)
Caused by: java.sql.SQLException: Invalid argument value, message from server: "Duplicate entry '0' for key 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1874)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1700)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1569)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:524)
... 12 more
It looks like hibernate is getting confused when updating the foreign keys in the record table, because the records of the first CSVFile does have the right foreign key, but there is no foreign key for the last CSVFile's records.
Can anyone please assit me?
Thanks in advance
Here is my code
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
in1 = new CSVFile();
s.save(in1);
Record record = new Record();
record.setField1("field1");
record.setField2("field2");
record.setField3("field3");
in1.addRecord(record);
s.save(record);
Record record1 = new Record();
record1.setField1("field111");
record1.setField2("field211");
record1.setField3("field311");
in1.addRecord(record1);
s.save(record1);
Record record2 = new Record();
record2.setField1("field4");
record2.setField2("field5");
record2.setField3("field6");
in1.addRecord(record2);
s.save(record2);
in2 = new CSVFile();
s.save(in2);
Record record3 = new Record();
record3.setField1("field7");
record3.setField2("field8");
record3.setField3("field9");
in2.addRecord(record3);
s.save(record3);
Record record4 = new Record();
record4.setId(4);
record4.setField1("field10");
record4.setField2("field11");
record4.setField3("field12");
in2.addRecord(record4);
s.save(record4);
s.save(in2);
tx.commit();
s.close();