-->
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.  [ 5 posts ] 
Author Message
 Post subject: Inserting Parent - Child with one to many relationship
PostPosted: Tue Jan 24, 2006 7:24 pm 
Newbie

Joined: Wed Nov 30, 2005 2:23 pm
Posts: 4
Hi All

I am having problem while saving parent and child object to the database. The relationship is one to many and only parent object is inserted in the daabase.

Hibernate version 3.1

Mapping file

<class name="com.elvis.metamodel.impl.Client" schema="Test" table="Client" lazy="false">
<id name="id" unsaved-value="undefined">
<generator class="assigned"></generator>
</id>
<property name="fName" column="FNAME" />
<property name="lName" column="LNAME" />
<property name="mInitial" column="MINITIAL" />
<property name="age" column="AGE" />
<list name="addresses" inverse="true">
<key column="parnt_id" not-null="true"/>
<index column="idx"/>
<one-to-many class="com.elvis.metamodel.impl.Address" />
</list>
</class>
<class name="com.elvis.metamodel.impl.Address" schema="Test" table="Address" lazy="false">
<id name="id" unsaved-value="undefined">
<generator class="assigned"></generator>
</id>
<property name="street" column="STREET" />
<property name="city" column="CITY" />
<property name="state" column="STATE" />
<property name="zipcode" column="ZIP" />
<many-to-one name="parent" column="parnt_id" not-null="true" class="com.elvis.metamodel.impl.Client"/>
</class>


I am calling Save method on session to save the object

here is the code

Client clt = new Client();
clt.setId(new Integer(1));
clt.setfName("Sunil");
clt.setlName("Pandit");
clt.setmInitial("B");
clt.setAge(34);
Address addr = new Address();
addr.setId(new Integer(2));
addr.setstreet("1700 Sunterro Drive");
addr.setcity("Austin");
addr.setstate("Texas");
addr.setzipcode("78727");
clt.addAddress(addr);
addr.setparent(clt);
objectaccess.getHibernateTemplate().save(clt);

I have also an interceptor which implements istransient method and it returns false. This as per doumented in Hibernate manual about assigned identifiers.

Can anyone point me what is missing ?

Thanks
Sunil


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 8:26 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The addresses list in Client has inverse="true" and therefore will not save addresses: it will only save relationships between Client and Address. Either save the Address first, or else remove the inverse="true" attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:47 pm 
Newbie

Joined: Wed Nov 30, 2005 2:23 pm
Posts: 4
Thanks for the reply

After removing Inverse=true , I get following error.
Unexpected row count: 0 expected: 1

Hibernate is trying to update address with the ID of client but there are no records inserted in address.

Thanks
Sunil


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:57 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
you have no cascade set, so you must save both objects.

Regards SEbastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 2:42 pm 
Newbie

Joined: Wed Nov 30, 2005 2:23 pm
Posts: 4
As described in Chapter 22 of Hibernate reference guide , the inver= true is required on collection so that it does not exeute the update statment. All this works as explained if the ID of the object is generated but does not work if the ID is assigned.
If I use cascade="all" , it still try to update the Address after insering client.

Any clue anyone ?

Sunil


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.