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

Bug 322579

Summary: EclipseLink EntityManager.createNamedQuery does not throw exception for invalid query names
Product: z_Archived Reporter: Oliver Drotbohm <odrotbohm>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED FIXED QA Contact:
Severity: major    
Priority: P2 CC: douglas.clarke, eclipselink.orm-inbox, michael.f.obrien, peter.krogh, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:
Attachments:
Description Flags
Testcase showing bug
none
Patch with test update on trunk stream none

Description Oliver Drotbohm CLA 2010-08-12 15:41:32 EDT
Build Identifier: 

JavaDoc of EntityManager says that createNamedQuery(String foo) throws an IllegalArgumentException "if a query has not been defined with the given name or …". Apparently the EclipseLink implementation does *not* throw an exception in this case. Attached you'll find a sample project triggering that call with some obviously stupid parameter. The EntityManager implementation returns a Query object that then in turn fails when being executed (by getResultList() e.g.).

Tested with all release versions >= 2.0.0 as well as 2.2.0 nightly build

Reproducible: Always

Steps to Reproduce:
1. Setup JPA infrastucture and obtain an EntityManager backed by Eclipse
2. Call createNamedQuery("somethingObviouslyStupid");
3. EntityManager returns a Query instance where according to the spec an exception is expected.
Comment 1 Oliver Drotbohm CLA 2010-08-12 15:42:42 EDT
Created attachment 176496 [details]
Testcase showing bug
Comment 2 Doug Clarke CLA 2010-08-12 17:33:26 EDT
Looks like we create the query wrapper and resolve it to the named query later. Should be a straight forward issue to resolve.
Comment 3 Oliver Drotbohm CLA 2010-08-13 02:53:30 EDT
(In reply to comment #2)
> Looks like we create the query wrapper and resolve it to the named query later.
> Should be a straight forward issue to resolve.

That's great news :). Just out of curiosity: doesn't that mean there's a glitch in the TCK if something (core) like this does show up during the certification?
Comment 4 Oliver Drotbohm CLA 2010-08-13 04:48:13 EDT
(In reply to comment #3)
> (In reply to comment #2)
> > Looks like we create the query wrapper and resolve it to the named query later.
> > Should be a straight forward issue to resolve.
> 
> That's great news :). Just out of curiosity: doesn't that mean there's a glitch
> in the TCK if something (core) like this does show up during the certification?

Of course I wanted to state "if something (core) like this does show *not* up during the certification?". in short: I would have expected the TCK to verify something like this (expected exceptions actually popping up).
Comment 5 Tom Ware CLA 2010-08-24 13:29:47 EDT
Setting target and priority see: http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines for more information on the meanings of these fields.
Comment 6 Oliver Drotbohm CLA 2010-08-24 13:39:35 EDT
(In reply to comment #5)
> Setting target and priority see:
> http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines for more
> information on the meanings of these fields.

Call me pedant but IMHO, incorrectly implementing the specification fulfills the attribution of "Required for Release. This fix is a requirement for the release listed in the target field.". I mean, EclipseLink is the reference documentation, so shouldn't such a violation of the contract of a very core interface considered a blocker?

Regards,
Ollie
Comment 7 Oliver Drotbohm CLA 2010-08-24 13:49:24 EDT
Of course I meant "reference implementation"…
Comment 8 Tom Ware CLA 2010-08-24 13:50:47 EDT
As with all our bugs, I would like to encourage those that feel a fix for this
bug should be a high priority to vote for it.  We do our best to address as
many as possible of the most voted on bugs with each release.
Comment 9 Tom Ware CLA 2010-08-24 15:32:59 EDT
'looks like there's a good amount of community support for fixing this bug.  Changing the target to the next release.
Comment 10 Doug Clarke CLA 2010-08-25 09:31:42 EDT
Proposed Patch:

Index: src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java
===================================================================
--- src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java	(revision 8004)
+++ src/org/eclipse/persistence/internal/jpa/EntityManagerImpl.java	(working copy)
@@ -997,7 +997,9 @@
     public Query createNamedQuery(String name) {
         try {
             verifyOpen();
-            return new EJBQueryImpl(name, this, true);
+            EJBQueryImpl query = new EJBQueryImpl(name, this, true);
+            query.getDatabaseQueryInternal();
+            return query;
         } catch (RuntimeException e) {
             setRollbackOnly();
             throw e;

A work-around for anyone blocked on this or using a released version of EclipseLink is to:

Query query = entityManager.createNamedQuery("query-name");

JPQ 1.0:
((EJBQueryImpl<?>) query).getDatabaseQueryInternal();

JPA 2.0        
query.getHints();
Comment 11 Oliver Drotbohm CLA 2010-08-25 10:06:11 EDT
(In reply to comment #10)
Hi Doug,

what does "proposed" mean in this case? Does the patch make it into next nightly integration build?

-- Ollie
Comment 12 Doug Clarke CLA 2010-08-25 12:12:23 EDT
I took a quick look and believe that is the correct patch. I just wanted to help out whoever ultimately fixes the bug and runs the regression suites.
Comment 13 Tom Ware CLA 2010-08-25 14:21:47 EDT
Created attachment 177454 [details]
Patch with test update on trunk stream

This patch will be checked into the trunk stream when tests finish.  The 2.1.x stream is currently closed for checkins until 2.1.1 ships.  A similar patch will be included in that stream when it opens.
Comment 14 Tom Ware CLA 2010-08-25 15:06:02 EDT
Fix above checked into trunk.

Reviewed by Guy Pelletier

Fixed RelationshipModelJUnitTestSuite.testNamedQueryDoesNotExistTest() to no longer call getResultList().

Tested with JPA LRG

2.1.x check in will occur when the stream opens.
Comment 15 Tom Ware CLA 2010-08-27 09:16:43 EDT
Checked into 2.1.2
Comment 16 Peter Krogh CLA 2010-08-30 09:25:07 EDT
This fix targets direct usage of the EclipseLink JPA implementation. When used within an EJB 3.0/3.1 container the container provider may provide a wrapping implementation or proxy.  In that case, it is up to the implementation to ensure that the named query is validated.
Comment 17 Eclipse Webmaster CLA 2022-06-09 10:31:16 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink