0

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?

4

1 回答 1

1

我不认为这是可能的。

顺便说一句,混合 GUI 层(SelectItem 类)和持久层(JPA)是一种不好的做法。我将使用适当的构造函数(long、String)创建单独的 StatusDTO 类,然后手动将其映射到 SelectItem。

通过这种方式,您将拥有独立于 GUI 更改的 DB 层更改。例如,如果数据库中的某些列将更改其类型,它不会影响您的 GUI 层,因为您可以在一个地方处理它 - DTO 类。否则,您将不得不遍历 SelectItem 等与数据库查询之间存在依赖关系的所有地方。

于 2013-05-10T13:22:28.253 回答