-->
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.  [ 1 post ] 
Author Message
 Post subject: one-to-one Relationship
PostPosted: Fri Mar 18, 2005 3:23 pm 
Newbie

Joined: Fri Mar 18, 2005 3:06 pm
Posts: 2
Location: St Louis
Hibernate version: 2.1
Name and version of the database you are using: Oracle 9i

I am new to Hibernate. Trying to experiment with it to see if it will save us development/maintenance time accessing our Oracle databases. Started with simple test structure of one table. Persistence went well. However, started experiencing difficulty with persisting data into a related table. In the experiment, I have just two tables that have a one-to-one relationship.

Table = Test_Sro
Sro_Number VarChar2 (Primary Key)
State Number
Working_Npa VarChar2
Working_Nxx VarChar2
Working_Line VarChar2

Table = Test_Caller
Sro_Number VarChar2 (Primary Key, Foreign Key back to Test_Sro.SroNumber)
Name VarChar2
Caller_Npa VarChar2
Caller_Nxx VarChar2
Caller_Line VarChar2

The Sro_Numebr is the primary key for both tables. There is a foreign key relationship between Test_Caller and Test_Sro. A row in Test_Caller cannot exist if the same Sro_Number doesn’t exist in the Test_Sro table.

I have three classes:
Sro {private String id; private int state; private Contact caller; private Tn workingTn;}
Tn {private String npa; private String nxx; private String line;}
Contact {private String name; private Tn tn;}
Each of the three classes have getters and setters for each of the attributes listed above. I used the Using the “Foreign key association example” in the Hibernate In Action book (page 221), but was unable to get records persisted to the Test_Caller table.

My JUnit code looks like this:
public void testSaveWithContact() throws Exception {
// Start session transaction
Session session = sessions.openSession(connection);
Transaction transaction = session.beginTransaction();
// Create SRO & set attributes
Sro sro = new Sro();
sro.setId("103");
sro.setState(2);
Tn tn = new Tn("314", "555", "1212");
sro.setWorkingTn(tn);

Contact caller = new Contact();
caller.setName("John Doe");
Tn callerTn = new Tn("636", "300", "4444");
caller.setTn(callerTn);
sro.setCaller(caller);
// Save the SRO and commit the database work.
session.save(sro, sro.getId());
transaction.commit();
session.close();
}

My initial mapping looked like this:
<hibernate-mapping>
<class name="HibernateTest.Sro" table="TEST_SRO">
<id name="id" column="SRO_NUMBER" type="string"> <generator class="native"></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"> <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>

I received the followed trace message when I tried to run the test:
net.sf.hibernate.MappingException: Error reading resource: SroMap.xml
Caused by: net.sf.hibernate.MappingException: Problem trying to set property type by reflection
Caused by: net.sf.hibernate.PropertyNotFoundException: field not found: id

I assumed it needed an “id” attribute in the Contact class. I’d prefer not do this since the class really doesn’t need the additional overhead. I added it anyway just to get the test working. After that, I received the following error though.
net.sf.hibernate.PropertyNotFoundException: Could not find a getter for sro in class HibernateTest.Contact

Based on the above exception/message it appears that Hibernate is expecting the Contact class to have a reference back to it’s parent class in order to persist the data. I reluctantly added a reference back to the Sro class within the Contact class and set the attribute/reference in the test.

When I run the test, a record was inserted to the Test_Sro table, but no record was inserted to the Test_Caller table. The following exception occurred:
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)

Can anyone help me build the map so that I don’t have to put references in the Contact class back to the Sro class and it’s id attribute?


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

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.