| Author |
inverse=true in many-to-many doubt
|
sudheer kiran
Ranch Hand
Joined: Jun 26, 2008
Posts: 237
|
|
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.
|
Sudheer
SCWCD, SCJP 5
|
 |
Christian Dillinger
Ranch Hand
Joined: Jul 20, 2009
Posts: 172
|
|
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
Joined: Jun 26, 2008
Posts: 237
|
|
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
Joined: Jun 26, 2008
Posts: 237
|
|
|
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
Joined: Jul 20, 2009
Posts: 172
|
|
In my sample Event is marked as inverse.
This code:
lists:
Please post your code and the result.
|
 |
sudheer kiran
Ranch Hand
Joined: Jun 26, 2008
Posts: 237
|
|
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
Joined: Jul 20, 2009
Posts: 172
|
|
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
Joined: Jun 26, 2008
Posts: 237
|
|
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
Joined: Jul 20, 2009
Posts: 172
|
|
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
Joined: Jun 26, 2008
Posts: 237
|
|
hi thanks for the help,now its working when i used session instead of DAOs.
|
 |
 |
|
|
subject: inverse=true in many-to-many doubt
|
|
|