Hibernate version:2.1
Hibernate mapping files Please see at the end of the post
I started playing with Hibernate recently so please treat me as layman....
Let me explain what I am doing. I have following
Table relations:
ADDRESS table
ADDRESSKEY
LINE1
LINE2
LINE3
CITY
STATE
ZIPCODE
Quote:
( ****Have Unique constraint on LINE1+ZIPCODE****)
CONTACT table
CONTACTKEY
CONTACTNAME
ADDRESSKEY (FK to AddressKey in ADDRESS table)
CONTACTTYPE
Object Structure :
AddressObject
ContactObject
Scenerio one: (works)
John Doe as CONTACT.CONTACTNAME
123 My Street as ADDRESS.LINE1
456 My City as ADDRESS.CITY
789 My State as ADDRESS.STATE
56789 as ADDRESS.ZIPCODE
When I try to set this sample data in Junit case and say
session.save(ContactObject) John Doe is saved in CONTACT table and address details are stored in ADDRESS table because I have
cascade="save-update" in
contact.hbm.xml file
Perfect world no proble with that ..........
Scenerio two: (doesn't work)
My new Junit case reads record inserted in
Scenerio one from ADDRESS table and loads it into
AddressObject.
I create
John Doe2 as CONTACT.CONTACTNAME.
ContactObject.setAddress(AddressObject);
//Basically same address details as Scenerio one
Then when I say
sess.save(ContactObject), it generates error message Unique constraint violation for LINE1+ZIPCODE. But What I want to do is not to insert one more record in ADDRESS table and use existing ADDRESSKEY. But how to do this?
Basic Question: How can I let Hibernate know that I want to store object hirarchy (AddressObject, ContactObject) in database only when similar records doesn't exist in ADDRESS table. If record exists in ADDRESS table then use existing ADDRESSKEY and insert into CONTACT as FK.
Pretty long description. Make sense! if not let me know ;-)
Hibernate mapping files
Address.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="data.Address" table="ADDRESS">
<id name="addresskey" type="long" unsaved-value="0">
<column name="ADDRESSKEY" not-null="true"/>
<generator class="sequence">
<param name="sequence">SEQ_ADDRESS</param>
</generator>
</id>
<property name="address_type" type="integer">
<column name="ADDRESS_TYPE" not-null="true" check="ADDRESS_TYPE IN(0,1,2)"/>
</property>
<property name="line1">
<column name="LINE1" not-null="true"/>
</property>
<property name="line2">
<column name="LINE2"/>
</property>
<property name="line3">
<column name="LINE3"/>
</property>
<property name="city">
<column name="city"/>
</property>
<property name="state">
<column name="STATE"/>
</property>
<property name="zipcode">
<column name="zipcode"/>
</property>
</class>
</hibernate-mapping>
Contact.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="data.Contact" table="CONTACT">
<id name="contactkey" type="long" unsaved-value="0">
<column name="CONTACTKEY" not-null="true"/>
<generator class="sequence">
<param name="sequence">SEQ_CONTACT</param>
</generator>
</id>
<many-to-one name="AddressObject" column="ADDRESSKEY" class="data.Address" cascade="save-update"/>
</class>
</hibernate-mapping>