Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Many to Many mapping in hibernate.

 
Dishpal Bhaluja
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
--------------------




 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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?
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • 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.
 
Arpit Purohit
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ya that worked.Thanks for the insightful comment.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic