Bug 296606 - issues with ElementCollections of Embeddables
Summary: issues with ElementCollections of Embeddables
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 296880 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-12-01 14:28 EST by James Sutherland CLA
Modified: 2022-06-09 10:27 EDT (History)
5 users (show)

See Also:


Attachments
The patch suggested bu James (slightly simplified) and the test. (5.81 KB, patch)
2009-12-03 13:18 EST, Andrei Ilitchev CLA
no flags Details | Diff
Take 2 - the patch originally suggested by James. (7.40 KB, patch)
2009-12-03 13:25 EST, Andrei Ilitchev CLA
no flags Details | Diff
patch - take 3 (10.33 KB, patch)
2009-12-04 12:19 EST, Andrei Ilitchev CLA
no flags Details | Diff
patch take 4 - adds fix for collection mebers' updates. (13.46 KB, patch)
2009-12-07 13:41 EST, Andrei Ilitchev CLA
no flags Details | Diff
Correction to the test - should make the test pass on server (7.11 KB, patch)
2009-12-08 11:21 EST, Andrei Ilitchev CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description James Sutherland CLA 2009-12-01 14:28:14 EST
In building a JPA 2.0 example I noticed some issues with the new ElementCollection mapping.

On any commmit the version of the Employee object owning an ElementCollection or EmailAddress objects is always incremented, even though there was no change.

This is due to the Employee emailAddress being a Map, and the change-set compare used in AggregateCollection always thinking everything changed for Map or Set.  The change-set compare needs to use the same logic as the database commit.

Also, with batch writing used, the Employee is not batched, as the Email is inserted one by one.

The issues is that aggregate-collections are not deferred like direct-collection nor inserted like entity objects.  Should be fixed to defer if the aggregate has no relationships (is simple).

We should also do the same for multiple table descriptors if the target table has no relationships to it.
Comment 1 James Sutherland CLA 2009-12-01 15:09:57 EST
Also noticed in own table creation we do not create a constraint in the element collection tables (direct-collection, nor aggregate-collection).

There is also no primary key constraint defined, but this may be ok (although we do create one on m-m's).
Comment 2 James Sutherland CLA 2009-12-02 14:18:24 EST
Ok, found a bigger issue, updates do not seem to work at all.

Any ElementCollection used with weaving on (default) will fail any update.

This is because aggregate-collections do not support change tracking, but the aggregate will default to using change tracking by default with weaving.

Aggregate mappings fix this by making the aggregate descriptor inherit the source descriptor change tracking setting in postInitialize, but aggregate-collection is not doing this.

The tests don't seem to find the issue as we seem to only have insert tests, no update tests.

A workaround is to set @ChangeTracking(DEFFERED) in the Embedded class.
Comment 3 Andrei Ilitchev CLA 2009-12-03 13:18:45 EST
Created attachment 153752 [details]
The patch suggested bu James (slightly simplified) and the test.
Comment 4 Andrei Ilitchev CLA 2009-12-03 13:25:06 EST
Created attachment 153755 [details]
Take 2 - the patch originally suggested by James.
Comment 5 Peter Krogh CLA 2009-12-03 13:32:50 EST
The above work around requires an EclipseLink specific annotation.

There is also a workaround that does not require EclipseLink to be on the
classpath to work (and is therefore JPA portable).

In the persistence.xml file add the following property:  

<property name="eclipselink.weaving.changetracking" value="false" />

The disadvantage of this workaround vs the annotation one, is that deffered
change tracking is put on the whole persistence unit, and may have an impact on
performance.
Comment 6 Tom Ware CLA 2009-12-03 14:08:05 EST
*** Bug 296644 has been marked as a duplicate of this bug. ***
Comment 7 Andrei Ilitchev CLA 2009-12-04 10:12:17 EST
*** Bug 296880 has been marked as a duplicate of this bug. ***
Comment 8 Andrei Ilitchev CLA 2009-12-04 12:19:39 EST
Created attachment 153826 [details]
patch - take 3

Updated the test - now it verifies both in cache and the data base, also tests updating of element collection member.
Comment 9 Tom Ware CLA 2009-12-07 09:58:05 EST
Setting target and priority.  For information about the meanings of these fields see:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 10 Andrei Ilitchev CLA 2009-12-07 13:41:59 EST
Created attachment 153948 [details]
patch take 4 - adds fix  for collection mebers' updates.

Mitesh found another issue that shows up when workaround applied and NPE is no longer thrown: collection members' updates don't show up in the database.

That happens because of Eclipselink's optimization - weaving primary key into object - that is applied to Embeddable collection target class.
Element collection of Embeddables is implemented in core by AggregateCollectionMapping, the target class reports all its attributes as components of primary key. When the object is created the woven pk is set, but when the object is altered the pk vowen pk is not updated - that's natural for entities where the pk is the real primary key, but that's not the case with AggregateCollection members - those can be altered during object's lifetime.

The fix is not to weave aggregate collection descriptors.

The patch also updates the test.
Comment 11 Andrei Ilitchev CLA 2009-12-07 15:31:18 EST
Checked into trunk (2.0.1). Reviewed by James.
Comment 12 Andrei Ilitchev CLA 2009-12-08 11:21:09 EST
Created attachment 154024 [details]
Correction to the test - should make the test pass on server
Comment 13 Andrei Ilitchev CLA 2010-01-06 14:46:21 EST
Ported to 2.0.1.
The bug is fixed in both trunk (2.1) and 2.0.1.
Comment 14 Eclipse Webmaster CLA 2022-06-09 10:27:33 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink