我有这个问题,替换函数无法使用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时,没有替换,它可以正常工作。
Obs: Eclipselink 2.5.2正在使用JPA2.1。我用的是Tomcat8,Eclipse开普勒。
发布于 2014-10-09 12:33:01
我找到了解决办法,谢谢你们提供的有用信息。
sql必须是这样的:
SELECT e FROM br.com.megasoft.protocolo.entity.Assunto e WHERE FUNCTION('REPLACE', e.titulo, '/', '') LIKE UPPER('A')这样,我将使用在JPA2.1规范中定义的函数。
发布于 2014-10-08 23:22:52
我已经在规范和这个甲骨文页面(fun)中搜索过了,没有任何JPQL函数的替换。
我相信这是一个Hibernate函数:http://lkumarjain.blogspot.com.br/2013/07/compare-string-by-removing-white-chars.html
换句话说,替换属于HQL,而不是JPQL。
发布于 2014-10-09 06:29:34
JPA2.1具有JPQL "FUNCTION“,它允许在JPQL语句中使用SQL函数。您使用的不是JPQL,因为它是对规范的简单回顾,或者JPA实现的文档会告诉您
https://stackoverflow.com/questions/26266384
复制相似问题