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?
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.