Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Adding many-to-many relationships using TopLink  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Sorry for not responding earlier, but thanks a lot for pointing me in the right direction.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!