This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue in Inserting data using One to Many

 
Selva Varadhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding



to the address side of the association.
 
Selva Varadhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John.

Regards,
Selva Varadahan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic