【问题标题】:Functions and Operators not working in Eclipselink函数和运算符在 Eclipse 中不起作用link
【发布时间】:2014-12-03 16:09:26
【问题描述】:

我有这个问题,REPLACE 功能不适用于 eclipselink 版本 2.5.2。

这是我的代码:

String sSql = " SELECT e FROM br.com.megasoft.protocolo.entity.Assunto e  WHERE  ( REPLACE(REPLACE(REPLACE( UPPER(e.titulo), '/', ''), '-', ''), '.', '') LIKE  UPPER('A') )";
TypedQuery<?> query = getEntityManager().createQuery(sSql, Class.forName(this.tabela));

第二个参数的值为:class br.com.megasoft.protocolo.entity.Assunto

它给出了这个例外:

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [ FROM br.com.megasoft.protocolo.entity.Assunto e  WHERE  ( REPLACE(REPLACE(REPLACE( UPPER(e.titulo), '/', ''), '-', ''), '.', '') LIKE  UPPER( :valorPesq10) ) ]. 
[54, 150] The expression is not a valid conditional expression.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 50 more

当我使用 Hibernate 时,它​​完全可以正常工作。

当我运行简单的 SQL 时,没有 REPLACE,它可以正常工作。

Obs:Eclipselink 2.5.2 正在使用 JPA 2.1。我正在使用 Tomcat8,Eclipse Kepler。

【问题讨论】:

    标签: java jakarta-ee jpa eclipselink


    【解决方案1】:

    我找到了解决办法,谢谢大家提供有用的信息。

    sql需要这样:

    SELECT e FROM br.com.megasoft.protocolo.entity.Assunto e  WHERE  FUNCTION('REPLACE',  e.titulo, '/', '') LIKE  UPPER('A')
    

    这样,我将使用 JPA 2.1 规范中定义的 FUNCTION

    【讨论】:

      【解决方案2】:

      我在 SPEC 和这个 oracle 页面 (http://docs.oracle.com/cd/E17904_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_string_fun) 中进行了搜索,JPQL 函数没有 REPLACE。

      我相信这是一个Hibernate函数:http://lkumarjain.blogspot.com.br/2013/07/compare-string-by-removing-white-chars.html

      也就是说,REPLACE 属于 HQL 而不是 JPQL。

      【讨论】:

        【解决方案3】:

        JPA 2.1 有 JPQL “FUNCTION”,它允许在 JPQL 语句中使用 SQL 函数。 您在那里使用的不是 JPQL 作为对规范的简单审查,或者您的 JPA 实现的文档会告诉您

        【讨论】:

        • 感谢您提供信息@Billy,您能帮我构建这个查询吗?我试过这样: SELECT e FROM br.com.megasoft.protocolo.entity.Assunto e WHERE FUNCTION('REPLACE', e.campo, '/', '') = UPPER('A') 仍然无法正常工作.
        • 什么是“不工作”?日志会告诉您正在调用什么 SQL,因此您可以根据该 SQL 来定义查询。
        猜你喜欢
        • 1970-01-01
        • 2012-06-24
        • 2022-06-27
        • 2018-02-26
        • 2017-05-03
        • 1970-01-01
        • 2015-09-27
        • 2022-06-21
        • 2017-04-01
        相关资源
        最近更新 更多