Hi,
Before this post I've read the parent child documentation and searched the form, but couldn't get the anwser to my problem.
The following link was usefull but it didn't solve my problem.
http://forum.hibernate.org/viewtopic.php?t=924744&highlight=automatically+save+children
I have a parent child relationship between TickTicket (parent) and TickOrderline (child). The relation between these objects is bi-directional.
In my code I create a parent object, a child object and then connect them together.
When hibernate tries to store the parent and the children into the database it first tries to insert the children (TickOrderline). This causes an exception due to the parent (TickTicket) does not exist.
It all runs in a transaction and when Hibernate calls the commit method an exception occurs.
Thanks,
Erwin.
Here are my files:
TickTicket.hbm.xml
Code:
<hibernate-mapping>
<class
name="com.lcmg.ticket.persist.persistobject.TickTicket"
table="tick_ticket"
>
<id
name="id"
type="java.lang.String"
column="id"
>
<generator class="uuid.hex"/>
</id>
<property
name="contentCode"
type="java.lang.String"
column="contentCode"
not-null="true"
length="255"
/>
<property
name="referenceId"
type="java.lang.String"
column="reference_id"
not-null="true"
length="32"
/>
<!-- bi-directional one-to-many association to TickOrderline -->
<set
name="tickOrderlines"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="ticket_id" />
</key>
<one-to-many
class="com.lcmg.ticket.persist.persistobject.TickOrderline"
/>
</set>
</class>
</hibernate-mapping>
TickOrderline.hbm.xml:
Code:
<hibernate-mapping>
<class
name="com.lcmg.ticket.persist.persistobject.TickOrderline"
table="tick_orderline"
>
<id
name="id"
type="java.lang.String"
column="id"
>
<generator class="uuid.hex"/>
</id>
<property
name="numTimesValid"
type="java.lang.Integer"
column="num_times_valid"
length="10"
/>
<property
name="numOfPersons"
type="int"
column="num_of_persons"
not-null="true"
length="10"
/>
<property
name="referenceId"
type="java.lang.String"
column="reference_id"
not-null="true"
length="32"
/>
<!-- bi-directional many-to-one association to TickTicket -->
<many-to-one
name="tickTicket"
class="com.lcmg.ticket.persist.persistobject.TickTicket"
not-null="true"
cascade="all"
>
<column name="ticket_id" />
</many-to-one>
</class>
</hibernate-mapping>
Stacktrace:
Code:
2004-03-09 15:38:17,468 WARN [main] [{testDoOrderMinimal}] net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 515, SQLState: 23000
2004-03-09 15:38:17,468 ERROR [main] [{testDoOrderMinimal}] net.sf.hibernate.util.JDBCExceptionReporter - [MTEST\MTESTDEVELOP1]Cannot insert the value NULL into column 'ticket_id', table 'MGRID_Ticketing.dbo.tick_orderline'; column does not allow nulls. INSERT fails.
2004-03-09 15:38:17,468 ERROR [main] [{testDoOrderMinimal}] net.sf.hibernate.util.JDBCExceptionReporter - could not insert: [com.lcmg.ticket.persist.persistobject.TickOrderline#8a9181b8fb300f3100fb300f3f880001]
java.sql.SQLException: [MTEST\MTESTDEVELOP1]Cannot insert the value NULL into column 'ticket_id', table 'MGRID_Ticketing.dbo.tick_orderline'; column does not allow nulls. INSERT fails.
at com.inet.tds.a.a(Unknown Source)
at com.inet.tds.b.new(Unknown Source)
at com.inet.tds.b.executeUpdate(Unknown Source)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2335)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.lcmg.ticket.domain.order.OrderTest.testCreateOrderSimple(OrderTest.java:218)
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 junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:395)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:279)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:171)
2004-03-09 15:38:17,468 ERROR [main] [{testDoOrderMinimal}] net.sf.hibernate.impl.SessionImpl - Could not synchronize database state with session
net.sf.hibernate.JDBCException: could not insert: [com.lcmg.ticket.persist.persistobject.TickOrderline#8a9181b8fb300f3100fb300f3f880001]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:478)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2335)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.lcmg.ticket.domain.order.OrderTest.testCreateOrderSimple(OrderTest.java:218)
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 junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:395)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:279)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:171)
Caused by: java.sql.SQLException: [MTEST\MTESTDEVELOP1]Cannot insert the value NULL into column 'ticket_id', table 'MGRID_Ticketing.dbo.tick_orderline'; column does not allow nulls. INSERT fails.
at com.inet.tds.a.a(Unknown Source)
at com.inet.tds.b.new(Unknown Source)
at com.inet.tds.b.executeUpdate(Unknown Source)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
... 22 more