Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » TRIM in JPA NamedQuery
TRIM in JPA NamedQuery [message #381079] Thu, 04 September 2008 03:04 Go to next message
Sven Hoffmann is currently offline Sven Hoffmann
Messages: 18
Registered: July 2009
Junior Member
I'm about to replace TopLink Essentials with EclipseLink 1.0.

At the moment I'm facing the problem that JPA NamedQueries where the
function TRIM is used result in a NullPointerException when parsing the
query.

Is this a known issue and is a workaround available? I did not find a bug
in Bugzilla nor a thread in this newsgroup.

Please find below the Entity, persistence.xml, usage of the EntityManager
and the resulting Stacktrace.

com.kn.persistence.Info.java:
package com.kn.persistence;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;

@Entity
@NamedQuery(name="Info.findByText", query="SELECT i FROM Info i WHERE
TRIM(i.text) = :text")
public class Info {
@Id
private long id;

private String text;

public Info() {
super();
}

public long getId() {
return this.id;
}

public String getText() {
return this.text;
}

@Override
public String toString() {
return this.getId() + "=" + this.getText();
}
}

com.kn.persistence.InfoReader.java:
package com.kn.persistence;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class InfoReader {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
try {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("testUnit");
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("Info.findByText");
query.setParameter("text", "test");

List<Info> infos = (List<Info>) query.getResultList();
for (Info info : infos) {
System.out.println(info.toString());
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="testUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider >
<class>com.kn.persistence.Info</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.jdbc.driver"
value="com.ibm.db2.jcc.DB2Driver"/>
<property name="eclipselink.jdbc.url"
value="jdbc:db2://host:port/database"/>
<property name="eclipselink.jdbc.password" value="XXX"/>
<property name="eclipselink.jdbc.user" value="XXX"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.cache.shared.default"
value="false"/>
</properties>
</persistence-unit>
</persistence>

Stacktrace:
[EL Finest]: 2008.09.04
08:58:04.311--ServerSession(199887850)--Thread(Thread[main,5 ,main])--End
deploying Persistence Unit testUnit; state Deployed; factoryCount 1
Local Exception Stack:
Exception [EclipseLink-8002] (Eclipse Persistence Services - 1.0 (Build
1.0 - 20080707)): org.eclipse.persistence.exceptions.JPQLException
Exception Description: General Problem parsing the query [Info.findByText:
SELECT i FROM Info i WHERE TRIM(i.text) = :text]. The parser returned the
following [null].
Internal Exception: java.lang.NullPointerException
at
org.eclipse.persistence.exceptions.JPQLException.generalPars ingException(JPQLException.java:116)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser .addError(JPQLParser.java:247)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser .parse(JPQLParser.java:135)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser .buildParseTree(JPQLParser.java:94)
at
org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQL DatabaseQuery(EJBQueryImpl.java:203)
at
org.eclipse.persistence.queries.JPAQuery.processJPQLQuery(JP AQuery.java:87)
at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl. deploy(EntityManagerSetupImpl.java:258)
at
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImp l.getServerSession(EntityManagerFactoryImpl.java:69)
at
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImp l.createEntityManagerImpl(EntityManagerFactoryImpl.java:118)
at
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImp l.createEntityManagerImpl(EntityManagerFactoryImpl.java:112)
at
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImp l.createEntityManager(EntityManagerFactoryImpl.java:100)
at com.kn.persistence.InfoReader.main(InfoReader.java:16)
Caused by: java.lang.NullPointerException
at
org.eclipse.persistence.internal.jpa.parsing.NodeFactoryImpl .newTrim(NodeFactoryImpl.java:669)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.trim(JPQLParser.java:10120)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.functionsReturningStrings(JPQLParser.java:9867)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.arithmeticPrimary(JPQLParser.java:8824)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.arithmeticFactor(JPQLParser.java:8620)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.arithmeticTerm(JPQLParser.java:8444)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.simpleArithmeticExpression(JPQLParser.java:8351)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.arithmeticExpression(JPQLParser.java:8292)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.simpleConditionalExpression(JPQLParser.java:7039)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.conditionalPrimary(JPQLParser.java:6997)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.conditionalFactor(JPQLParser.java:2989)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.conditionalTerm(JPQLParser.java:2879)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.conditionalExpression(JPQLParser.java:2805)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.whereClause(JPQLParser.java:2762)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.selectStatement(JPQLParser.java:356)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQL Parser.document(JPQLParser.java:261)
at
org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser .parse(JPQLParser.java:133)
... 9 more
Re: TRIM in JPA NamedQuery [message #381083 is a reply to message #381079] Fri, 05 September 2008 02:23 Go to previous messageGo to next message
Sven Hoffmann is currently offline Sven Hoffmann
Messages: 18
Registered: July 2009
Junior Member
Same exception occurs with EclipseLink 1.0.1 M3.
Re: TRIM in JPA NamedQuery [message #381270 is a reply to message #381079] Mon, 08 September 2008 03:51 Go to previous messageGo to next message
Sven Hoffmann is currently offline Sven Hoffmann
Messages: 18
Registered: July 2009
Junior Member
I just found that changing TRIM(i.text) to TRIM(FROM i.text) in the JPA
query solved the problem.

According to the BNF specification of TRIM the keyword FROM is not
required.
Chapter 4.14 BNF:
[...]
functions_returning_strings ::=
[...]
TRIM([[trim_specification] [trim_character] FROM] string_primary)|
[...]
Re: TRIM in JPA NamedQuery [message #381273 is a reply to message #381270] Mon, 08 September 2008 09:49 Go to previous messageGo to next message
James is currently offline James
Messages: 272
Registered: July 2009
Senior Member
Thanks for digging into this
Sounds like a bug, please log this issue in the Bugzilla.

-- James
Re: TRIM in JPA NamedQuery [message #381276 is a reply to message #381273] Mon, 08 September 2008 11:30 Go to previous message
Sven Hoffmann is currently offline Sven Hoffmann
Messages: 18
Registered: July 2009
Junior Member
Please see https://bugs.eclipse.org/bugs/show_bug.cgi?id=246598

-- Sven
Previous Topic:Exception [EclipseLink-30005] - Persistence FAILED
Next Topic:enum collection
Goto Forum:
  


Current Time: Wed Apr 16 09:43:04 EDT 2014

Powered by FUDForum. Page generated in 0.11075 seconds