This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Many to Many mapping in hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Many to Many mapping in hibernate." Watch "Many to Many mapping in hibernate." New topic
Author

Many to Many mapping in hibernate.

Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
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

Joined: Aug 16, 2007
Posts: 1374
Hello Dishpal,

I would ask to check your hibernate mapping files twice. How come user is directly connected with department?
Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
I didnt get you.I have included my every file for this configuration.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Check your hibernate mapping files. It doesn't seem to be correct.
Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
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

Joined: Jun 28, 2008
Posts: 210
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.


Rahul Babbar
Arpit Purohit
Greenhorn

Joined: Jan 09, 2007
Posts: 21
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.


Regards,
Arpit Purohit
Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
Ya that worked.Thanks for the insightful comment.


 
 
subject: Many to Many mapping in hibernate.
 
Similar Threads
Hibernate Repeated column mapping problem
Classcast exception
Error when inserting data in hibernate
Mapping Error Hibernate
Delete/update associations question