This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inverse=true in many-to-many doubt

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have two hbm files person and event
i have declared a many-to-many relation on both files and declared inverse="true" in event.hbm.xml
every thing is working fine on Person
but in event none of these are working
1---event.setParticipants(set);
2---event.getParticipants().add(person);


if i remove inverse="true" then its working.
what actually is inverse.
thanks.
 
Christian Dillinger
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post your mappings?

This works for me:





To maintain both side of the association. In Person:


In Event:
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thanks for the reply,its working but what if you wanted to add a collection of participants to event.
event.setParticipants(participants); is not working
am i need to use a for loop.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which ever side i declare inverse=true,on that side(say event.hbm.xml or person.hbm.xml ) the setCollection() method doesnt inserting any values.
 
Christian Dillinger
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my sample Event is marked as inverse.

This code:


lists:



Please post your code and the result.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Event.hbm.xml


Person.hbm.xml


my class method


after i compile,there are no errors,
only select statements are displayed nto insert or updates.
no change to data base.

 
Christian Dillinger
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code:


It's obvious that event.setParticipants(participants) cannot trigger an insert when Event is mapped as inverse. Because Hibernate ignores changes made on that set. You can implement setParticipants(participants) like that:



So you change the list on the non-inverse-side. Hibernate should trigger the inserts then.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
working fine but still i got another issue
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [hb.dao.Event#3]


entries in person_event table are
1-1
1-2
1-3

when i try to insert 2-2 or 2-3 i get that exception
 
Christian Dillinger
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That exception occurs when you try to put an entity that is either in detached state or read by another session (let's say sessionA) into sessionB where it is already "known". So Hibernate cannot decide which version it should handle as current state. (Merging is the solution...)
But without seeing the complete code it's not possible to find a real solution for that particular case.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thanks for the help,now its working when i used session instead of DAOs.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic