• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Many to Many mapping in hibernate.

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Table1 myUser =userId userName
Table2 myDept = deptId deptName
Table3 myUserDept=userId deptId

Which when reverese mapping using Hibernate generates a myUser.hbm and myDept.hbm and both of it having a Set element to insert myUserDept data into .

So in my tester class I am loading a myUser object with its primary key field value and inserting two deptId's using set and committing the transaction,But when I do soHibernate is calling a delete on mytable myUserDept and then tries to insert the values in the Set where the orginal functionality I needed was just inserting

The server output is



Is this the problem in the way I am updating the table (in my tester class).Or Am I missing my Basics here.Kindly resolve my Issue.

Code Part
--------------------
Tester class


Mydept.hbm.xml b/w hibernate-mapping
--------------------------------------------------


Myuser.hbm.xml b/w hibernate-mapping
-----------------------------------------------

hibernate.cfg.xml between hibernate-config
--------------------




 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dishpal,

I would ask to check your hibernate mapping files twice. How come user is directly connected with department?
 
Dishpal Bhaluja
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get you.I have included my every file for this configuration.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your hibernate mapping files. It doesn't seem to be correct.
 
Dishpal Bhaluja
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remapped the entire thing still Its coming like that only.I guess the problem is with how I am inserting the value.
In the case Im creating a new UserObject rather than loading it from database,Its correctly inserting to both tables.
Where as when I use a load an extraneous delete is found.
How can I get rid of this?
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes....i think the problem is coming because you are doing
user.setMydepts(new HashSet());
According to this code, if there was anything in the original set, it should be removed....and it should be replaced by this new Set...
So, hibernate is doing what it should, remove the original records, and then add the new records...
I have not tested it, but some code like below should work.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Babbar wrote:Yes....i think the problem is coming because you are doing
user.setMydepts(new HashSet());
According to this code, if there was anything in the original set, it should be removed....and it should be replaced by this new Set...
So, hibernate is doing what it should, remove the original records, and then add the new records...
I have not tested it, but some code like below should work.



I agree with Rahul.
In general, we do not use user.setMydepts(new HashSet()) as this method is to be used by hibernate internally to populate the HashSet.
You are calling this statement after the HashSet is already been populated by Hibernate.
 
Dishpal Bhaluja
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya that worked.Thanks for the insightful comment.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic