Issue Details (XML | Word | Printable)

Key: NUCJPA-190
Type: Bug Bug
Status: Closed Closed
Resolution: Cannot Reproduce
Priority: Major Major
Assignee: Unassigned
Reporter: Marcel Overdijk
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
DataNucleus API JPA

Using ParameterExpression causes unexpected exception

Created: 25/Oct/12 12:55 PM   Updated: 31/Dec/12 07:59 PM   Resolved: 25/Nov/12 10:43 AM
Component/s: None
Affects Version/s: 3.1.1, 3.1.2
Fix Version/s: None

File Attachments: 1. Zip Archive Archive.zip (2 kB)



 Description  « Hide
I'm using Datanucleus with Spring Data JPA on App Engine.
The basis persisting and queries go well. But as I was exploring further I stumbled against a problem with ParameterExpressions. Which does not seem to work.

When having:

{code}
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> root = cq.from(User.class);
ParameterExpression<String> username = cb.parameter(String.class);
cq.select(root);
cq.where(cb.like(root.<String> get("username"), username));
TypedQuery<User> tq = em.createQuery(cq);
tq.setParameter(username, "man" + "%");
List<User> users = tq.getResultList();
{code}

See also full test case I will attach after creating this issue.

The error I get is:

INFO: >> Exception thrown retrieving objects
java.lang.IllegalArgumentException: Query has parameter "-1" supplied at execution yet the query doesnt make use of this parameter
at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:461)
at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:419)
at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:56)
at org.datanucleus.test.Main.main(Main.java:108)
Caused by: org.datanucleus.store.query.QueryInvalidParametersException: Query has parameter "-1" supplied at execution yet the query doesnt make use of this parameter
at org.datanucleus.store.query.Query.applyImplicitParameterValueToCompilation(Query.java:881)
at org.datanucleus.store.query.Query.setImplicitParameter(Query.java:828)
at org.datanucleus.store.rdbms.query.JPQLQuery.setImplicitParameter(JPQLQuery.java:136)
at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:457)
... 3 more


Note that with Spring Data JPA I was getting a little bit different exception, but I don't know the exact code created by Spring Data JPA.
Nevertheless when experimenting with the query builder/criteria I found above issue.


Btw this is the error I see with Spring Data JPA:
Numbered parameter syntax starting ? but isnt followed by numeric!; nested exception is javax.persistence.PersistenceException: Numbered parameter syntax starting ? but isnt followed by numeric!

Marcel Overdijk made changes - 25/Oct/12 12:56 PM
Field Original Value New Value
Attachment Archive.zip [ 11797 ]
Marcel Overdijk added a comment - 25/Oct/12 01:00 PM
Could this be related to NUCJPA-188 ?

As I'm seeing this my logs (note the missing space between ?-1ORDER):

11:27:15,215 DEBUG [DataNucleus.Query] - JPQL Query : Compiling "SELECT FROM org.mycomp.domain.User DN_THIS WHERE DN_THIS.username LIKE ?-1ORDER BY DN_THIS.username ASC"

Exception:
javax.persistence.PersistenceException: Numbered parameter syntax starting ? but isnt followed by numeric!


Andy Jefferson added a comment - 25/Oct/12 01:22 PM
CriteriaQuery doesn't support positional parameters as per the JPA spec 3.8.13 ("The use of positional parameters is not supported for criteria queries."). Presumably what that means is it is left to an implementation to decide if they want to support it; anyway its not supported.

i.e the call to
ParameterExpression<String> username = cb.parameter(String.class);
which specifies no "name" and so is not a "named" parameter, hence is "positional". But then if you use the method specifying the parameter name you likely would have no problem.

Marcel Overdijk added a comment - 25/Oct/12 03:16 PM
Hmm,

When I check my logging I also see other queries form Spring Data to DataNucleus using ?-1 position parameters and they cause no problem until now.
Maybe this is by accident. What I can see when executing a like ?-1 query without order that this query is automatically converted to >= and < query, and hence the positional params are gone.

This isn't the case for when having an order by clause, but probably this is not resolved as ths spacing issue NUCJPA-188?

Is the fix for NUCJPA part of SNAPSHOT build so I can try that out?

Andy Jefferson added a comment - 25/Oct/12 03:30 PM
NUCJPA-188 would be in nightly builds, and affects use of criteria to generate the JPQL string query, with missing space after filter and also having clauses.

Marcel Overdijk added a comment - 25/Oct/12 03:52 PM
OK, I tried the SNAPSHOT but stumbled against another (regression) issue? I put some comments in NUCJPA-188.

Andy Jefferson added a comment - 25/Nov/12 10:43 AM
Can't see this on 3.2.0.m1

Andy Jefferson made changes - 25/Nov/12 10:43 AM
Status Open [ 1 ] Resolved [ 5 ]
Resolution Cannot Reproduce [ 5 ]
Andy Jefferson made changes - 31/Dec/12 07:59 PM
Status Resolved [ 5 ] Closed [ 6 ]