2014-10-08 57 views
2

我有這個問題,替換函數不工作與eclipselink版本2.5.2。功能和操作符不工作在Eclipselink

這裏是我的代碼:

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,它工作正常。

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

回答

2

我找到了解決方案,謝謝你們提供的有用信息。

的SQL語句需要這樣:

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

這樣,我將使用功能JPA 2.1規範定義

1

JPA 2.1擁有JPQL「FUNCTION」,它允許在JPQL語句中使用SQL函數。 你使用什麼沒有JPQL作爲規範的簡單回顧,或者你的JPA實現的文檔會告訴你

+0

感謝您提供的信息@比利,你能幫我建立這個查詢嗎?我嘗試過這種方式:SELECT e FROM br.com.megasoft.protocolo.entity.Assunto e WHERE FUNCTION('REPLACE',e.campo,'/','')= UPPER('A') Still不工作。 – 2014-10-09 12:00:36

+0

什麼是「不工作」?日誌告訴你正在調用什麼SQL,因此你可以從這個工作開始定義查詢。 – 2014-10-09 12:22:28