Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Hibernate Many to Many association does not update database  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17344
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ahmet Degirmenci
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!