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

Bug 345478

Summary: Incorrect foreign key parameter set when retrieving an eager @OneToMany
Product: z_Archived Reporter: Rafal Swierzynski <szczyp>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED FIXED QA Contact:
Severity: critical    
Priority: P2 CC: adrian.goerler, eclipselink.orm-inbox, jhasenbe, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   
Whiteboard:
Attachments:
Description Flags
test case
none
proposed fix - trunk
none
updated fix - trunk none

Description Rafal Swierzynski CLA 2011-05-11 13:51:05 EDT
Build Identifier: Eclipse Persistence Services - 2.2.0.v20110202-r8913

There are Parent and Child classes, where Parent has a collection of Child elements, and Child has a Parent (bidirectional OneToMany). The following test code fails miserably:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("testUnit");
EntityManager em = emf.createEntityManager();

em.getTransaction().begin();
Parent p = new Parent("parent");
p = em.merge(p);
em.getTransaction().commit();
em.clear();

Child child = new Child("child", p);
em.getTransaction().begin();
child = em.merge(child);  // <-- here
em.getTransaction().commit();

emf.close();

with the following stack trace:

[EL Warning]: 2011-05-11 19:19:26.027--UnitOfWork(2019462525)--Thread(Thread[pool-1-thread-1,5,main])--Local Exception Stack: 
Exception [EclipseLink-6094] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.QueryException
Exception Description: The parameter name [ID] in the query's selection criteria does not match any parameter name defined in the query.
Query: ReadAllQuery(name="children" referenceClass=Child sql="SELECT ID, NAME, PARENT_ID FROM CHILD WHERE (PARENT_ID = ?)")
	at org.eclipse.persistence.exceptions.QueryException.parameterNameMismatch(QueryException.java:1050)
	at org.eclipse.persistence.internal.expressions.ParameterExpression.getValue(ParameterExpression.java:246)
	at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.translate(DatabaseCall.java:951)
	at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:206)
	at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
	at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:264)
	at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:647)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2558)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2517)
	at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:410)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1080)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:808)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
	at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:383)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1126)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2842)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1521)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1503)
	at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.instantiate(QueryBasedValueHolder.java:98)
	at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.instantiateForUnitOfWorkValueHolder(QueryBasedValueHolder.java:113)
	at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiateImpl(UnitOfWorkValueHolder.java:156)
	at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiate(UnitOfWorkValueHolder.java:222)
	at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:88)
	at org.eclipse.persistence.indirection.IndirectSet.buildDelegate(IndirectSet.java:192)
	at org.eclipse.persistence.indirection.IndirectSet.getDelegate(IndirectSet.java:343)
	at org.eclipse.persistence.indirection.IndirectSet.size(IndirectSet.java:500)
	at test.Parent.toString(Parent.java:45)
	at java.lang.String.valueOf(String.java:2826)
	at java.lang.StringBuilder.append(StringBuilder.java:115)
	at test.Child.toString(Child.java:47)
	at java.lang.String.valueOf(String.java:2826)
	at org.eclipse.persistence.queries.ObjectLevelModifyQuery.toString(ObjectLevelModifyQuery.java:267)
	at java.text.MessageFormat.subformat(MessageFormat.java:1246)
	at java.text.MessageFormat.format(MessageFormat.java:836)
	at java.text.Format.format(Format.java:140)
	at java.text.MessageFormat.format(MessageFormat.java:812)
	at org.eclipse.persistence.internal.localization.EclipseLinkLocalization.buildMessage(EclipseLinkLocalization.java:77)
	at org.eclipse.persistence.internal.localization.TraceLocalization.buildMessage(TraceLocalization.java:30)
	at org.eclipse.persistence.logging.AbstractSessionLog.formatMessage(AbstractSessionLog.java:852)
	at org.eclipse.persistence.logging.DefaultSessionLog.log(DefaultSessionLog.java:156)
	at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:3069)
	at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:4185)
	at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:4157)
	at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:4133)
	at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:4055)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1497)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
	at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:224)
	at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:123)
	at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:3766)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1404)
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:616)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1511)
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:264)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1130)
	at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
	at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
	at test.MergeTest.test(MergeTest.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:640)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:627)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:799)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1103)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:137)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:121)
	at org.testng.TestRunner.runWorkers(TestRunner.java:1098)
	at org.testng.TestRunner.privateRun(TestRunner.java:727)
	at org.testng.TestRunner.run(TestRunner.java:581)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:315)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:310)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:272)
	at org.testng.SuiteRunner.run(SuiteRunner.java:221)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:40)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:83)
	at org.testng.internal.thread.ThreadUtil$CountDownLatchedRunnable.run(ThreadUtil.java:151)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:662)


Is works with previous versions, most notably the last pre-2.2.x version, 2.1.2.


This is a blocker and a regression. Because of this bug, we cannot upgrade to GlassFish 3.1 final or further, and GlassFish 3.0.1 final is unusable due to a hight rate of bugs and memory leak problems (related to Weld, Javassist, ...).

Reproducible: Always

Steps to Reproduce:
1. unpack the test case
2. go to the directory
3. mvn clean test
Comment 1 Rafal Swierzynski CLA 2011-05-11 13:52:05 EDT
Created attachment 195399 [details]
test case
Comment 2 Tom Ware CLA 2011-05-20 08:40:11 EDT
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 3 Tom Ware CLA 2011-07-06 11:39:52 EDT
A couple of comments about your recreation - unforunately they don't solve the issue though :(

JPA requires you to set both sides of a relationship.  As a result, your Child constructor should be something like this:

    public Child(String name, Parent parent) {
        this.name = name;
        this.parent = parent;
        this.parent.children.add(this);
    }

Whe you call merge on the child, parent is detached, as a result, you'll need to either explicitly merge the parent or set child.parent to cascade MERGE

As I mention above, these don't solve the problem, but are things to keep in mind in your app.
Comment 4 Tom Ware CLA 2011-07-06 15:33:05 EDT
Issue seems related to the lack of a shared cache.  Workaround is to enable the shared cache.
Comment 5 Tom Ware CLA 2011-07-08 11:03:05 EDT
Created attachment 199340 [details]
proposed fix - trunk
Comment 6 Tom Ware CLA 2011-07-08 14:57:34 EDT
Created attachment 199357 [details]
updated fix - trunk
Comment 7 Tom Ware CLA 2011-07-08 15:13:08 EDT
*** Bug 337422 has been marked as a duplicate of this bug. ***
Comment 8 Tom Ware CLA 2011-07-11 09:53:30 EDT
Fixed in 2.2.x, 2.3.x and trunk

Fix adds a flag that makes a temporary CacheKey act isolated - this behavior already exists for other temporary CacheKeys

Reviewed by Gordon Yorke

Added test to CacheableJunitTest

Tested with Core and JPA LRG
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:28:25 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink