aspose file tools*
The moose likes Object Relational Mapping and the fly likes Issue in Inserting data using One to Many Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Issue in Inserting data using One to Many" Watch "Issue in Inserting data using One to Many" New topic
Author

Issue in Inserting data using One to Many

Selva Varadhan
Greenhorn

Joined: Aug 20, 2005
Posts: 14
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 [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.

[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

---------------------
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>

Regards,
Selva varadhan
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Try adding



to the address side of the association.
Selva Varadhan
Greenhorn

Joined: Aug 20, 2005
Posts: 14
Hi John,
Now i 'm getting the expected result,Thanks.
If i know the logic behind it, it will be more helpful for me.


Regards,
Selva Varadhan.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
inverse="true" says this association is the reverse side of a bi-directional association.

Only the non-inverse side of the association will be used to sync the in-memory object with the database.
Selva Varadhan
Greenhorn

Joined: Aug 20, 2005
Posts: 14
Thanks John.

Regards,
Selva Varadahan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue in Inserting data using One to Many
 
Similar Threads
bidirectional one to many mapping issues
hibernate 1-many insertion
Hibernate 3.0
hibernate creating duplicate ForeignKey index
Inserting data in Intermediate table on the Many-to-Many