Bug 395738 - Bad row mapping when join fetching an abstract entity with column discriminator
Summary: Bad row mapping when join fetching an abstract entity with column discriminator
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P2 critical with 6 votes (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard: submitted_patch
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2012-12-04 12:11 EST by J LM CLA
Modified: 2022-06-09 10:23 EDT (History)
5 users (show)

See Also:


Attachments
Maven project with JPA Model, and JUnit tests to reproduce the issue (12.34 KB, application/octet-stream)
2012-12-04 12:11 EST, J LM CLA
no flags Details
Quick Patch on 2.3.3 sources (1.19 KB, patch)
2012-12-04 20:22 EST, J LM CLA
no flags Details | Diff
Test case refreshed (7.28 KB, application/octet-stream)
2014-11-27 13:43 EST, J LM CLA
no flags Details
Patch for EclipseLink 2.6.0-M3 (1.74 KB, patch)
2014-11-27 14:00 EST, J LM CLA
no flags Details | Diff
jUnit tests integrated into EclipseLink (35.85 KB, patch)
2014-12-03 13:18 EST, Tomas Kraus CLA
no flags Details | Diff
Final fix and jUnit changeset pushed into EclipseLink master (41.76 KB, patch)
2014-12-04 09:39 EST, Tomas Kraus CLA
tomas.kraus: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description J LM CLA 2012-12-04 12:11:41 EST
Created attachment 224270 [details]
Maven project with JPA Model, and JUnit tests to reproduce the issue

Assume I have a model with an Entity E1 with 2 onetoone on another Entity E2.
E2 is an abstract class (@Inheritance) extended by 2 Entites E21 and E22. They use a discriminator column. This case is a design choice and doesn't sound too messy (to me ).

When retrieving an E1 object (find by id, select in jpql or criteria), the two onetoone objects are retrieved and values of their fields are correct.

As it triggers 3 sql queries, I want to optimize by join fetching the 2 onetoone : tried with jpql, criteria and JoinFetch annotation.

Now one of the two retrieved E2 objects (whatever their subtypes) have incorrect fields : either the two are the same, either one of them is null.

The sql generated contains distinct fields for each entity but values retrieved are badly mapped to the objects.

Only workaround I see, would be to demultiply a onetoone on the abstract class to multiple onetoeones to concrete subclasses. Not really a good solution.

In the attachment, I have placed a project based on maven (pom.xml).
You will find :
in src/main/java/, the classes of the JPA model : sample with a Customer having two addresses that can be sub-typed.
In src/test/java, you will find 3 classes (DBCreateEL, InsertEL, ReadEL).
In the stderr log, when launching ReadEL, you will see the values retrieved and incorrectly setted with join fetch cases (JPQL and Criteria).

Find with ID / Select JPQL / NO FETCHES CRITERIA :
--------------------------------------------------
The SQL :
SELECT CUS_ID, CUS_ACCOUNT, ADD_ID_BA, ADD_ID_DA FROM PRO_T_CUSTOMER WHERE (CUS_ID = ?)
SELECT ADD_ID, ADD_TYPE, STREET1, STREET2 FROM PRO_T_ADDRESS WHERE (ADD_ID = ?)
SELECT ADD_ID, ADD_TYPE, STREET1, STREET2 FROM PRO_T_ADDRESS WHERE (ADD_ID = ?)

The objects :
Customer [51,ACCNUMBER,
AddressType1 [streetT1],
AddressType2 [streetT2]]

WITH FETCHES JPQL :
-------------------
The SQL :
SELECT t1.CUS_ID, t1.CUS_ACCOUNT, t1.ADD_ID_BA, t1.ADD_ID_DA, t0.ADD_ID, t0.ADD_TYPE, t0.STREET1, t0.STREET2, t2.ADD_ID, t2.ADD_TYPE, t2.STREET1, t2.STREET2 FROM PRO_T_CUSTOMER t1 LEFT OUTER JOIN PRO_T_ADDRESS t0 ON (t0.ADD_ID = t1.ADD_ID_DA) LEFT OUTER JOIN PRO_T_ADDRESS t2 ON (t2.ADD_ID = t1.ADD_ID_BA) WHERE (t1.CUS_ID = ?)

The objects :
Customer [51,ACCNUMBER,
AddressType1 [null],
AddressType2 [streetT2]]

WITH FETCHES CRITERIA :
-----------------------
The SQL :
SELECT DISTINCT t1.CUS_ID, t1.CUS_ACCOUNT, t1.ADD_ID_BA, t1.ADD_ID_DA, t0.ADD_ID, t0.ADD_TYPE, t0.STREET1, t0.STREET2, t2.ADD_ID, t2.ADD_TYPE, t2.STREET1, t2.STREET2 FROM PRO_T_CUSTOMER t1 LEFT OUTER JOIN PRO_T_ADDRESS t0 ON (t0.ADD_ID = t1.ADD_ID_BA) LEFT OUTER JOIN PRO_T_ADDRESS t2 ON (t2.ADD_ID = t1.ADD_ID_DA) WHERE (t1.CUS_ID = ?)

The objects :
Customer [51,ACCNUMBER,
AddressType1 [streetT1],
AddressType2 [null]]

In the two last cases, values of addresses are incorrect.
We can see that the order of join declaration affects data mapping. As we inversed the order in the two cases (I think code is clearer than me ;-) ). 


Note :
The bug exists with 2.4.0, but I have placed the version 2.3.3 as I need a patch based on 2.3.x branch (for compatibility with toplink grid 12c).
Comment 1 J LM CLA 2012-12-04 15:52:59 EST
Found an issue when looking the joinedMappingIndexes of the JoinManager (cas of the fetches with Criteria). (Used by trimRowForJoin)

[null, null, org.eclipse.persistence.mappings.OneToOneMapping[billingAddress]=4, null, null, null, null, org.eclipse.persistence.mappings.OneToOneMapping[deliveryAddress]=6, null, null, null, null, null, null, null, null]

The index for OneToOneMapping[deliveryAddress] should be 8 instead of 6.
So fields of the billingAddress are still in the row and are used by in favour of the ones of deliveryAddress.
Comment 2 J LM CLA 2012-12-04 16:16:29 EST
It seems that the compute joinedindexes doesn't take into account that the classes are extended an abstract entiy with its own fifield and in my case a discriminator column (so +2).
Comment 3 J LM CLA 2012-12-04 20:22:57 EST
Created attachment 224285 [details]
Quick Patch on 2.3.3 sources

Attached a patch on the JoinedAttributeManager class.
The computeIndexesForJoinedExpressions method only treats multitableinheritance, which in my case is not enough.
I've added a simple else if, but hiding the details of the condition in the ObjectExpression like for MultiTableInheritance would be better.
Comment 4 J LM CLA 2012-12-11 07:40:21 EST
The patch is compatible with 2.4.x
Not tested on 2.5.x
Comment 5 Tom Ware CLA 2013-02-12 08:59:23 EST
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 6 J LM CLA 2013-03-25 05:54:13 EDT
The attached Patch resolves the issue.
Comment 7 Tom Ware CLA 2013-03-25 07:58:28 EDT
Why are you closing this bug?  Is the patch somehow checked into the code stream?
Comment 8 J LM CLA 2013-06-26 01:27:16 EDT
Not at all, bug is still present and easy to fix.
Wondering if status needed to be changed to indicate that the patch is ok.
Comment 9 Tom Ware CLA 2013-06-26 08:22:25 EDT
We leave bugs open until the fix is checked into the appropriate EclipseLink code stream.  Then we close them with the corresponding target release.
Comment 10 Tomas Kraus CLA 2014-11-27 04:07:18 EST
Please can you run this test with latest master branch build (2.6.0) and port this fix to master branch too? I wanted to apply your fix on master branch myself but it failed.
Comment 11 J LM CLA 2014-11-27 13:43:41 EST
Created attachment 248984 [details]
Test case refreshed

I provide a simpler Test case, referencing the 2.6.0-M3 version of EclipseLink :
Entity Customer with 2 sub entities Address (BillingAddress and DelliveryAddress).

In the Junit CustomerTest, 2 tests (criteria and jpql without join fetch) succeed and 2 others fail (criteria and jpql with join fetch).

Just run a "mvn test" command to view the failures.
Comment 12 J LM CLA 2014-11-27 14:00:18 EST
Created attachment 248985 [details]
Patch for EclipseLink 2.6.0-M3

Previous submitted patch was based on EclipseLink 2.3.3.
This one is technically the same patch but realigned with sources of version 2.6.0-M3.
Comment 13 Tomas Kraus CLA 2014-12-03 13:18:23 EST
Created attachment 249144 [details]
jUnit tests integrated into EclipseLink

Here are tests integrated into JPA jUnit tests.
I had to create new model under org.eclipse.persistence.testing.models.jpa.advanced.customer and rename some of the entities to get unique names.

Please review my changes and let me know if you agree with pushing your original test with my modifications into EclipseLink repository.
Comment 14 Tomas Kraus CLA 2014-12-03 13:27:06 EST
Also, please, sign CLA and sign off your 'Patch for EclipseLink 2.6.0-M3' patch properly as described on https://wiki.eclipse.org/CLA and upload it again.
Comment 15 Tomas Kraus CLA 2014-12-04 09:39:24 EST
Created attachment 249169 [details]
Final fix and jUnit changeset pushed into EclipseLink master

Pushed into master:

http://git.eclipse.org/c/eclipselink/eclipselink.runtime.git/commit/?id=65064541a4ef538059e54f28b14d5f9d75d50c25
Comment 16 Tomas Kraus CLA 2014-12-04 09:42:57 EST
Fis shall be available in next master nightly build.
Comment 17 Eclipse Webmaster CLA 2022-06-09 10:23:27 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink