在JPQL查询中使用LIKE,PERCENT和CONCAT的一般方法

时间:2011-01-18 09:58:54

标签: jpa eclipselink jpa-2.0 jpql

我正在我的JEE6-app中进行一些JPA 2.0查询,我在其中使用带参数的命名查询。我当前的查询只是一个参数的“全部查找”查询。

@NamedQuery(name = "UserPE.byAny", query = "SELECT b FROM UserPE b WHERE " +
            "b.email LIKE CONCAT('%', :PARAM, '%') OR " +
            "b.name LIKE CONCAT('%', :PARAM, '%') OR " + 
            "b.firstName LIKE CONCAT('%', :PARAM, '%')")

这个JPQL查询适用于Hibernate 3.5.4-final。

现在我切换到Eclipselink 2.1.0-RC3(也试过2.1.1,没有成功),这似乎有问题我如何使用concatlike。我在尝试使用相同的查询时遇到...unexpected token [CONCAT]...的例外情况。

完整的堆栈跟踪:

Exception [EclipseLink-8025] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [UserPE.byAny: SELECT b FROM UserPE b WHERE b.email LIKE CONCAT('%', :PARAM, '%') OR b.name LIKE CONCAT('%', :PARAM, '%') OR b.firstName LIKE CONCAT('%', :PARAM, '%')], line 1, column 42: unexpected token [CONCAT].
Internal Exception: NoViableAltException(16!=[743:1: likeValue returns [Object node] : (n= literalString | n= inputParameter );])
    at org.eclipse.persistence.exceptions.JPQLException.unexpectedToken(JPQLException.java:372)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.handleRecognitionException(JPQLParser.java:319)
    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.jpa.parsing.jpql.antlr.JPQLParser.likeValue(JPQLParser.java:8501)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.likeExpression(JPQLParser.java:8356)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionWithNotExpression(JPQLParser.java:7866)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.simpleConditionalExpressionRemainder(JPQLParser.java:7750)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.simpleConditionalExpression(JPQLParser.java:7625)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalPrimary(JPQLParser.java:7561)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalFactor(JPQLParser.java:4507)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalTerm(JPQLParser.java:4397)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalExpression(JPQLParser.java:4323)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.whereClause(JPQLParser.java:4280)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectStatement(JPQLParser.java:371)
    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.JPAQuery.processJPQLQuery(JPAQuery.java:106)
    at org.eclipse.persistence.internal.jpa.JPAQuery.prepare(JPAQuery.java:90)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:464)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:430)
    at org.eclipse.persistence.internal.sessions.AbstractSession.processJPAQueries(AbstractSession.java:1747)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:409)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:671)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:620)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:368)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
    at de.comap.controller.AbstractDAO.<init>(AbstractDAO.java:22)
    at de.comap.controller.GroupDAO.<init>(GroupDAO.java:20)
    at de.comap.controller.GroupDAOTest.beforeClass(GroupDAOTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:640)
    at mockit.integration.testng.internal.TestNGTestRunnerDecorator.invokeMethod(TestNGTestRunnerDecorator.java:78)
    at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:503)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:193)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:123)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:183)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:115)
    at org.testng.TestRunner.runWorkers(TestRunner.java:1009)
    at org.testng.TestRunner.privateRun(TestRunner.java:683)
    at org.testng.TestRunner.run(TestRunner.java:553)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:311)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:306)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:268)
    at org.testng.SuiteRunner.run(SuiteRunner.java:217)
    at org.testng.TestNG.runSuite(TestNG.java:1062)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:956)
    at org.testng.TestNG.run(TestNG.java:874)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:74)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:92)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: NoViableAltException(16!=[743:1: likeValue returns [Object node] : (n= literalString | n= inputParameter );])
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.likeValue(JPQLParser.java:8465)
    ... 61 more

这是查询如何与EclipseLink一起使用:

@NamedQuery(name = UserPE.BY_ANY, query = "SELECT b FROM UserPE b WHERE " +
                                          "b.email LIKE %:PARAM% OR " +
                                          "b.name LIKE %:PARAM% OR " + 
                                          "b.firstName LIKE %:PARAM%")

但是这个查询再次不适用于Hibernate。是否有适用于所有JPA提供商的通用方法?或者这个区域的规格太模糊了吗?

我真的希望我的查询尽可能独立于JPA实现。

2 个答案:

答案 0 :(得分:3)

JPA规范说:

  

pattern_value 是字符串文字或字符串值   输入参数

因此,使用标量函数的能力是特定于提供者的扩展。

我认为唯一可行的方法是在传递参数之前在Java代码中执行连接。

答案 1 :(得分:1)

您的错误是Eclipse Persistence Services - 2.0.0.v20091127,而不是2.1,

这应该适用于2.1。

JPQL规范不允许带有LIKE的函数,因此这是特定于提供者的。