Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 325035

Summary: Duplicate Insert on lazy Set
Product: z_Archived Reporter: Bernard Missing name <bht237>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED FIXED QA Contact:
Severity: blocker    
Priority: P2 CC: andrei.ilitchev, eclipselink.orm-inbox, michael.f.obrien, syvalta, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Whiteboard:
Attachments:
Description Flags
Testcase: NetBeans Project in ZIP File
none
Standalone testcase - not using GlassFish container
none
proposed fix
none
updated patch
none
updated patch none

Description Bernard Missing name CLA 2010-09-11 15:20:41 EDT
Build Identifier: Eclipse Persistence Services - 2.0.1.v20100213-r6600)

This is a simple case where a performance setting unexpectedly changes application program logic.

I am getting duplicate database inserts with

@Entity
public class MainEntity{

@OneToMany(fetch=FetchType.LAZY)
private Set<ForeignEntity> foreigners;

when I do mainEntity.getForeigners().add(foreigner);

It works fine when I change to @OneToMany(fetch=FetchType.EAGER),
or when I check with if(! foreigners.contains(foreigner)) before add().

It looks like add() is not fetching the lazy Set from the database.

The attached testcase reproduces the behavior.


Reproducible: Always

Steps to Reproduce:
Please refer to the attached testcase (NetBeans project)
Comment 1 Bernard Missing name CLA 2010-09-11 15:23:09 EDT
Created attachment 178686 [details]
Testcase: NetBeans Project in ZIP File
Comment 2 Bernard Missing name CLA 2010-09-12 21:45:03 EDT
Created attachment 178710 [details]
Standalone testcase - not using GlassFish container

The attached testcase reproduces with weaving enabled.
Comment 3 Tom Ware CLA 2010-09-20 08:28:58 EDT
Setting target and priority.  See the following page for details of the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 4 Andrei Ilitchev CLA 2010-09-23 15:58:47 EDT
Workaround: pass to createEMF method (or add to persistence.xml) property  "eclipselink.weaving.changetracking" with value "false".

Proposed solution: add a new PersistemnceUnitProperty to switch off IndirectCollections optimization, something like:
"eclipselink.indirect-collection.avoid-instantiation"
with values "true" - default and "false".
Comment 5 Tom Ware CLA 2010-11-18 09:21:36 EST
Deferring - did not make 2.1.2 cutoff and likely will not make 2.2 cutoff - for now, the workaround listed above can be used.
Comment 6 Tom Ware CLA 2010-12-09 09:14:24 EST
See also: Bug 331938 - likely related
Comment 7 Tom Ware CLA 2011-06-02 14:02:42 EDT
The issue appears to be related to cascade persist.  We do not actually get the Set itself wrong, but we cascade the persist before we check the list existence.  This results in us trying to persist a new version of OfferRegion.

Workaround is to avoid cascadePersist on the offer->offerRegion mapping.
Comment 8 Tom Ware CLA 2011-06-06 13:56:11 EDT
Created attachment 197435 [details]
proposed fix
Comment 9 Tom Ware CLA 2011-06-07 14:50:46 EDT
Created attachment 197532 [details]
updated patch
Comment 10 Tom Ware CLA 2011-06-07 15:30:37 EDT
Too late for 2.3 check-in.  Fix will appear in 2.3.1.
Comment 11 Tom Ware CLA 2011-06-07 15:31:05 EDT
Created attachment 197538 [details]
updated patch
Comment 12 Tom Ware CLA 2011-06-08 10:15:08 EDT
Fix makes IndirectSet instantiate on add/remove by default and allows enable/disable of this functionality on IndirectSet and IndirectList.

Reviewed by James Sutherland

Added tests to: org.eclipse.persistence.testing.tests.jpa.inheritence.EntityManagerJUnitTestSuite

Tested with Core and JPA LRG
Comment 13 Tom Ware CLA 2011-06-09 15:04:00 EDT
Reopening.  Still requires check-in to 2.3.1 stream when it opens.
Comment 14 Tom Ware CLA 2011-06-29 13:16:39 EDT
Checked into 2.3.1
Comment 15 Chris Delahunt CLA 2012-11-21 15:01:01 EST
*** Bug 315040 has been marked as a duplicate of this bug. ***
Comment 16 Eclipse Webmaster CLA 2022-06-09 10:21:30 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink