Bug 332309 - Deleting in a ManyToManyRelation with a JoinTable with referencedColumn != name in JoinColumn is not working
Summary: Deleting in a ManyToManyRelation with a JoinTable with referencedColumn != na...
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P2 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-10 09:25 EST by bertram.walter CLA
Modified: 2022-06-09 10:25 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description bertram.walter CLA 2010-12-10 09:25:34 EST
Build Identifier: 2.2.0-M5

Having a relation like:

@ManyToMany
@JoinTable(name = "JoinTable", joinColumns = @JoinColumn(name = "EntityA_Id", referencedColumnName = "referenceId"),
inverseJoinColumns = @JoinColumn(name = "EntityB_Id", referencedColumnName = "id"))
public List<EntityB> getEntityBs(){...}

class A {
int id; // primary key
int referenceId; // This is the id of a EntityB
}
class B {
int id; //primary key
}
// Hope the above is correct as it created from my mind.

works during adding of an object but for deleting an object from the list.
In the later case the resulting SQL is something like this:
DELETE FROM JoinTable WHERE ((EntityB_Id = 40) AND (EntityA_Id= null))
The problem is the 'null' value which is because in
foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/mappings/ManyToManyMapping.java in method 
 protected void objectRemovedDuringUpdate(ObjectLevelModifyQuery query, Object objectDeleted, Map extraData) throws DatabaseException, OptimisticLockException

the call of 
prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getSession())
is missing just before  
AbstractRecord databaseRow = this.mechanism.buildRelationTableSourceAndTargetRow(query.getTranslationRow(), unwrappedObjectDeleted, query.getSession(), this);

In all other cases (methods objectOrderChanged...(), insertAddedObject...()) this call is present and at least for the addition I know that it works.

I added the mentioned call, compiled a new version, run test (ant test-core-srg, test-core, test-srg, test-lrg) and tested in my application and it works with my application so far (table is empty and SQL statement is correct). No idea about any side-effects etc.


Reproducible: Always
Comment 1 Tom Ware CLA 2010-12-23 11:41:12 EST
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Richard Richter CLA 2016-01-08 03:14:26 EST
We've just encountered this issue as well. It generally does NOT work, if the referencedColumnName references non-@Id column of the owning entity. I cannot find anything in JPA 2.1 specification suggesting it should not work and there are even good reasons when it should work. That non-@Id field can be some business unique identifier that was (natural) @Id before and we want to introduce surrogate key as new @Id now. All the other relations shouls still work, but it suddenly stops deleting relations between many-to-many entities.

Test case can be run in shell very easily:
$ svn export https://github.com/virgo47/litterbin/trunk/issues/eclipselink332309
$ cd eclipselink332309
$ mvn clean package

Build fails on running test. Test creates two Dogs and an Owner and adds both dogs to owner.dogs Set (commit). In next transaction it removes one dog from the set and commits again. This produces delete like this:
DELETE FROM Owner_Dog WHERE ((dog_id = ?) AND (owner_uniqid = ?)) {1: 1, 2: NULL};

As reported originally in 2010, problem is NULL on owner_uniqid which should have value of owner.uniqId column. In the test I assert that it is NOT null. Delete itself runs happily, but asserting count of dogs for the owner shows the problem.

The test itself is on my github:
https://github.com/virgo47/litterbin/tree/master/issues/eclipselink332309

svn export is the fastest way how to get it run

Also you can switch to hibernate, just comment/uncomment provider lines in persistence.xml and run mvn clean package again - it will pass this time.
Comment 3 Richard Richter CLA 2016-01-08 03:56:42 EST
BTW: Tested with version 2.6.2
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:25:03 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink