This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate Many to Many association does not update database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Many to Many association does not update database" Watch "Hibernate Many to Many association does not update database" New topic
Author

Hibernate Many to Many association does not update database

Ahmet Degirmenci
Greenhorn

Joined: Jan 02, 2010
Posts: 2
I am trying to insert record to a database using hibernate many to many association. There is something I miss but could not figure it out.
Here are my files

User.hbm.xml




MusicType.hbm.xml


The code piece that is supposed to make update(which does not)



however the following code does :


The persistenceManager and hu is instances of same Class : HibernateUtils the update function is here


My POJO s and database is generated by hibernate tools

If you have an idea, please guide me. Thanks.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

It is all about managed persistence. In the second example the related object is being managed by Hibernate. In the first example it is not.

In the first example you would be required to set both sides of the relationship. You many to many mapping is bi-directional, and in your first example you set only one of those directions.

In the second example since both sides of the association is loaded via the Session and therefore both being managed, they have both sides set.

So because only one side is set in the first example, it only knows how to update that side of the relationship and not the other side. The other side would update that side of the relationship in the database.

Hope that helps clear things up.

Basically if you add

currentUser.getLikedMusicTypes().add(selected);
selected.setUser(currentUser);

instead, then you set both sides and the persist call will update the MusicTypes side.

The best approach is to have an user.addMusicType(User user) or musicType.addUser(MusicType) method on the code and in the add method make sure that the other side is also set.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ahmet Degirmenci
Greenhorn

Joined: Jan 02, 2010
Posts: 2
After reading your post and the part of Hibernate Documentation about bi-directional associations I understand the problem.
(Also realized that the "inverse" in the User.hbm.xml should not be there at all)
Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Many to Many association does not update database
 
Similar Threads
Hibernte will delete rows from a table, but it won't insert rows
Hibernate - Force foreign key lookup on insert
Hibernate 3 Not Loading Values Into Child Set In JUnit Test
If somebody can translate this for me please?
1-to-M mapping confusion