• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding many-to-many relationships using TopLink

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I'm running into some trouble adding many-to-many relationships, using TopLink Essentials 2.0.1 (Build b09d-fcs (12/06/2007). I have two entity classes, Person and File, like this:


Using this, three tables were generated, as I had expected:



I have a single File entity in my FILE table (with ID = 3054) and two Person entities (IDs 4 and 2956) in PERSON. There is a link between the File and the Person with ID 4 in FILE_PERSON. Now when I want to create a link between the File and the second Person, I try this:



where DBParams provides a Hashmap with the connection details and FileHelper and PersonHelper are classes with some static functions that do queries.

The result is this:




The link between Person with ID 4 and File with ID 3054 is the one already in the database. I think that TopLink tells MySQL to create all the links, but since the first one is already there, there is a constraint violation and it does a rollback. Am I right? And is there a way to skip the already existing link and only create the new one?

Thanks for any help!
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When defining a bi-directional ManyToMany mapping you need to use a "mappedBy' on one side.

See,
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Duplicate_rows_inserted_into_the_join_table.

That is probably the issue, it may also be relate to you only maintaining one side of the relationship and mixing managed and non-managed objects.
You should also look up or merge each person from the EntityManager context if they were read from a different context (different EntityManager or JTA transaction). Or call merge() on the existing File if it were read from a previous persistence context.
 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not responding earlier, but thanks a lot for pointing me in the right direction.
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic