Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Issue in Inserting data using One to Many

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John.

Regards,
Selva Varadahan
reply
    Bookmark Topic Watch Topic
  • New Topic