Bob
Bob

Reputation: 5628

Casting in JPQL when using NEW

I'm using Eclipselink, and I have the following JPQL query:

SELECT NEW javax.faces.model.SelectItem((java.lang.Object) s.id, s.description) FROM Status s WHERE s.active = TRUE

The cast before s.id is needed because the constructor of SelectItem expects an Object and a String, but Status.id is a long.

However, when executing this query I get the following exception:

Caused by: Exception [EclipseLink-8024] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [SELECT NEW javax.faces.model.SelectItem((java.lang.Object) s.id, s.description) FROM Status WHERE s.active = TRUE], line 1, column 59: syntax error at [s].
Internal Exception: MismatchedTokenException(78!=82)
    at org.eclipse.persistence.exceptions.JPQLException.syntaxErrorAt(JPQLException.java:362)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.handleRecognitionException(JPQLParser.java:304)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.addError(JPQLParser.java:245)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.reportError(JPQLParser.java:362)
    at org.eclipse.persistence.internal.libraries.antlr.runtime.BaseRecognizer.recoverFromMismatchedElement(Unknown Source)
    at org.eclipse.persistence.internal.libraries.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken(Unknown Source)
    at org.eclipse.persistence.internal.libraries.antlr.runtime.BaseRecognizer.mismatch(Unknown Source)
    at org.eclipse.persistence.internal.libraries.antlr.runtime.BaseRecognizer.match(Unknown Source)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.constructorExpression(JPQLParser.java:2635)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectExpression(JPQLParser.java:2045)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectItem(JPQLParser.java:1351)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectClause(JPQLParser.java:1266)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectStatement(JPQLParser.java:352)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.document(JPQLParser.java:276)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.parse(JPQLParser.java:133)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.buildParseTree(JPQLParser.java:94)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:198)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:173)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:125)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:109)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1326)
    ... 51 more
Caused by: MismatchedTokenException(78!=82)
    ... 66 more

How can I cast s.id to java.lang.Object without getting an exception?

Upvotes: 0

Views: 283

Answers (1)

Piotr Kochański
Piotr Kochański

Reputation: 22682

I don't think it is possible.

By the way it's a kind of bad practice to mix up GUI layer (SelectItem class) and persistence layer (JPA). I would create separate StatusDTO class with proper constructor (long, String) and then manually map it to SelectItem.

In this way you will have DB layer changes independent on GUI changes. For instance if some column in database will change its type, it would not affect your GUI layer, since you can take care of it in one place - DTO class. Otherwise you would have to go through all places where you have dependency between SelectItem, etc. and DB queries.

Upvotes: 1

Related Questions