aspose file tools*
The moose likes Object Relational Mapping and the fly likes Can't update a many-to-many relatonship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can Watch "Can New topic
Author

Can't update a many-to-many relatonship

Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 114
Hi all,

I have in my model two entities which are reciprocally many-to-many related. I have for both entities List instances which hold their associated values. When adding referencies to both lists I am getting no error at all, but when trying to detach references, the code works fine,no error is thrown and if the server not being restarted, the results persist throughout the life of the session. However when checking the database the values are still there (Join Table) , and when restarting the server the detached references are there again! I have been struggling with this for a while already.

Here are the code pieces that establish the relationship in my orm.xml file:





In the database (MySQL) I have two separate tables, and one JOIN table which holds the id's for both entities:



and finally let me post my utilitary persistence method:



This is roughly what it takes to work, but since I am relatively new to the JSF and the PersistenceFramework, I believe experienced users may notice something not matching. could anybody explain me why this wouldn't work?In One-To-Many and reverse relationships I am not having this kind of problem at all. I can't understand why when the session is alive, changes are tracked in the obectmodeltree, not updating the database though the fush() call being there. And cannot understand why although clearing the PersistenceContext on each call, the changes persist in the object tree if it's not been updated to the database. This is all pretty confusing.

Thanks for your time and apologise for the length of this post.

Regards,
Carlos Conti.
Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 114
Well, found a partial solution, which I dislike, but enables me to move forward.

After doing more research, I saw a thread in Stackoverflow which said that the owner of the relationship should trigger the actions of removing the associated references in order for the join column in the database to update. So what I did was to exchange the mapping-by attribute and join column definitions (int the previous topic are the relevant code pieces) from one entity to the other one.

This unexpectedly solved my problem!, however the next question is to my discourage, what if I would like to remove the references from the other entity. According to the explanation mentioned it wouldn't work. Therefore I would like to ask the forum, is there any way of building such a relationship and performing UPDATE actions with independence of which entity owns the many-to-many relationship?

Regards,
Carlos Conti.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
Please let me know the reason, why your persist method is static synchronized(class level lock). Can you make synchronized block instead of static synhronized ?
Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 114
Hello Kri Shan,

to be honest I am not an experienced user regarding multithreading. As I said I am dealing in a very simplistic way with persistence, namely executing all code against that method. From what I know I could synchronize the relevant block instead of the whole method, however due to the short lenght of the executed code and its effects in other parts of the application, I don't see a big difference. However, I don't see a reason why you couldn't make a synchronized block instead, as long as you enclose within the block the beginning and end of the transaction you are executing, and flush calls of course.

I need to use it this way, because I manage a paralel thread which is responsible for sending notifications via e-mail and other devices within our organisation when an event triggers it. Since I use the same persistence context and entity manager for retrieving relevant info, I must ensure two different operations do not try to make use of the entity manager at the same time.

If you have any insights I will be glad to know!
Regards,
Carlos.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't update a many-to-many relatonship