-->
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.  [ 9 posts ] 
Author Message
 Post subject: A classic problem(atleast for me!) session.save(ObjectTree)
PostPosted: Sat Oct 16, 2004 5:39 pm 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
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>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 9:43 pm 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
in the Scenerio two, what is the id of the address before save?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 1:53 am 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
id is the next value from sequence SEQ_ADDRESS.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 5:36 am 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
sure, but is the same as the address of the first part?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 8:20 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Try to find address by "LINE1+ZIPCODE" before to "ContactObject.setAddress(AddressObject);" and create new one if address doe's not exist. Is it a problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 9:19 am 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
exactly......

I do that with following

Query query = sess.createQuery("from address a where a.zipcode=:zc and a.line1=:line1");
query.setString("zc", "89765");
query.setString("line1", "something address1");
List result1 = query.list();
Address ad = (Address) result1.get(0);
System.out.println(ad.getAddresskey()); // This prints addresskey so I know that address already exists

if (addresskey = null) {
ContactObject.setAddress(AddressObject);
sess.save(ContactObject); // This will save address and contact
}
else{
/** what should I do here such that only contact object gets stored with addresskey of ad.getAddresskey() **/
}

;-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 2:40 pm 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
if (addresskey = null) {
ContactObject.setAddress(AddressObject);
sess.save(ContactObject);
}else{
ContactObject.setAddress(ad);
sess.save(ContactObject); // This will save
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 5:41 pm 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
if (addresskey = null) {
ContactObject.setAddress(AddressObject);
sess.save(ContactObject);
}else{
ContactObject.setAddress(ad);
sess.save(ContactObject); // This will save
}


I assume where ad is the Address object/record which is already stored in database so ad.getAddresskey() will return not null value.

I did that already ..... it still says unique key violation because it tries to create new record in ADDRESS table ! This is where I am confused...:)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 10:41 pm 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
Got it..... Thanks for your help


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

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.