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.  [ 7 posts ] 
Author Message
 Post subject: Issu in insering data in One-to-Many
PostPosted: Thu Oct 20, 2005 2:30 pm 
Newbie

Joined: Wed Sep 28, 2005 10:13 am
Posts: 4
Hi all,

I 'm trying the mapping for One-to-Many( One Customer has many Address).
with the foreign key relationship in the Address Table

I have collection of Address in Customer.
then i 'm inserting the Customer using sessionVar.save(customer)
So I 'm getting the following query and Data was inserted fine.

But my issue here is, on the line numbers of the query [2]and [3], address data been inserted with the foreign key [CUSID],even then it is firing two more quries to update CUSID.

I feel it is unnecessary. With this posting i have given my mapping files for both address and Customer. Plz guide me if anything i need to do to avoid this.


Hibernate version: 3.0

Mapping documents:
---------------------
Mapping file for Customer

<hibernate-mapping package="mapping/one2many/set">
<class name="Customer" table="CUSTOMER" lazy="true" >
<id name="cusId" type="long" column="CID">
<generator class="increment"/>
</id>
<property name="name" column="NAME" access="field" />
<set name="addresses" table="ADDRESS" cascade="all">
<key column="CUSID"/>
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>

-----------------------------
Mapping file for

<hibernate-mapping package="mapping/one2many/set">
<class name="Address" table="ADDRESS" proxy="Address" lazy="true" >
<id name="addId" type="long" column="ADDID">
<generator class="increment"/>
</id>
<property name="address" column="ADDRESS"/>
<many-to-one name="customer" column="CUSID" cascade="all" class="Customer" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

txn = ses.beginTransaction();
Customer person = new Customer();
person.setName("Abc");

Address info = new Address();
info.setAddress("Address");

Address info2 = new Address();
info2.setAddress("Address2");

person.setItem(info);
person.setItem(info2);

ses.save(person);
txn.commit();

Full stack trace of any exception that occurs:

Name and version of the database you are using: HQL

The generated SQL (show_sql=true):


[1]insert into CUSTOMER (NAME, CID) values ('ABC', 4)
[2]insert into ADDRESS (ADDRESS, CUSID, ADDID) values ('Address', 4, 7)
[3]insert into ADDRESS (ADDRESS, CUSID, ADDID) values ('Address2',4, 8)
[4]update ADDRESS set CUSID=4, POSITION=0 where ADDID=7
[5]update ADDRESS set CUSID=4, POSITION=1 where ADDID=8

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:34 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Looks like you need to add inverse="true" to your <set name="addresses"...>, and remove the cascade attribute from the <many-to-one> on the other side.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:38 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
Add to addresses set mapping inverse="true" and do not forget assign customer object that owns that address to customer property on address. Then it will not be updating addresses.

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject: Issue in inserting data in One-to-Many
PostPosted: Fri Oct 21, 2005 12:45 am 
Newbie

Joined: Wed Sep 28, 2005 10:13 am
Posts: 4
Hi Nathan,Basilio
now i 'm getting the expected result. Thanks.
If i get the logic behind it, it will be more useful for me.

Regards,
Selvaraj.V


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 9:52 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
This chapter in the docs explains it all: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#example-parentchild

_________________
nathan


Top
 Profile  
 
 Post subject: Re: Issue in inserting data in One-to-Many
PostPosted: Fri Oct 21, 2005 12:15 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
selva_raj wrote:
Hi Nathan,Basilio
now i 'm getting the expected result. Thanks.
If i get the logic behind it, it will be more useful for me.

Regards,
Selvaraj.V


In general inverse="true" means that other side will be responsible to create relationship (assign foreign key value). Other words before address will be inserted FK will be assigned.

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 11:36 pm 
Newbie

Joined: Wed Sep 28, 2005 10:13 am
Posts: 4
Hi nathan and Basilio,

Now i understood the concept of inverse="true" thanks.

Regards,
Selva varadhan


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