I receive the below exception when trying to do some simple testing with Hibernate. I'm new to Hibernate and just trying to get a feel for how it works and how we might be able to take advantage of it. The persistence works OK when I have just one table mapped (Test_Sro), but I get the below exception as soon as I try to add a new class and associated table using a one-to-one relationship.
For this example, the Test_Sro table is the parent table and the Test_Caller is the child table. The Test_Sro table maps to the Sro class, adn the Test_Caller table maps to the Contact class. The Test_Caller should have a primary key assigned that is equal to the primary key of the Test_Sro record inserted to the database.
In the below code, a row is inserted to the Test_Sro table, but nothing is inserted to the Test_Caller table.
Can anyone help with how my mapping needs to be changed?
Hibernate version: 2.1.7c
Mapping documents:
<hibernate-mapping>
<class name="HibernateTest.Sro" table="TEST_SRO">
<id name="id" column="SRO_NUMBER" type="integer"><generator class="increment"></generator></id>
<property name="state" type="integer" column="STATE" />
<component name="workingTn" class="HibernateTest.Tn">
<property name="npa" type="string" column="WORKING_NPA" />
<property name="nxx" type="string" column="WORKING_NXX" />
<property name="line" type="string" column="WORKING_LINE" />
</component>
<one-to-one name="caller" class="HibernateTest.Contact" cascade="all"/>
</class>
<class name="HibernateTest.Contact" table="TEST_CALLER">
<id name="id" column="SRO_NUMBER" type="integer"><generator class="foreign"><param name="property">sro</param></generator></id>
<property name="name" column="NAME" type="string"/>
<component name="tn" class="HibernateTest.Tn">
<property name="npa" type="string" column="CALLER_NPA" />
<property name="nxx" type="string" column="CALLER_NXX" />
<property name="line" type="string" column="CALLER_LINE" />
</component>
<one-to-one name="sro" class="HibernateTest.Sro" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session session = sessions.openSession(connection);
Transaction transaction = session.beginTransaction();
// Create SRO & set attributes
Sro sro = new Sro();
sro.setState(2);
Tn tn = new Tn("314", "555", "1212");
sro.setWorkingTn(tn);
Contact caller = new Contact();
caller.setName("John Doe");
caller.setSro(sro);
Tn callerTn = new Tn("636", "300", "4444");
caller.setTn(callerTn);
sro.setCaller(caller);
// Save the SRO and commit the database work.
session.save(sro);
transaction.commit();
session.close();
Full stack trace of any exception that occurs:
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:663)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:623)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at HibernateTest.TestSro.testSaveWithContact(TestSro.java:123)
at java.lang.reflect.Method.invoke(Native Method)
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:329)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:218)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:151)
Name and version of the database you are using: Oracle 9i
|