Case insensitive search in hibernate

时间:2017-02-03 10:21:15

标签: sql hibernate jpa orm

我想执行一个名为hibernate的查询,但它给出了hibernate语法异常。在sql中它正常运行..chart将作为查询参数和查询值= 'chart'

SELECT * FROM faq WHERE UPPER(answer) LIKE UPPER('%chart%')

select f from Faq f where lower(f.question) like lower('%:query%') or lower(f.answer) like lower('%:query%')

@NamedQuery(name = "findQuesAnsByQuery", query = "select f from Faq f where lower(f.question) like lower('%:query%') or lower(f.answer) like lower('%:query%')")

跟踪:

[0] = {java.lang.StackTraceElement@16677}"org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)"
[1] = {java.lang.StackTraceElement@16678}"org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)"
[2] = {java.lang.StackTraceElement@16679}"org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)"
[3] = {java.lang.StackTraceElement@16680}"org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)"
[4] = {java.lang.StackTraceElement@16681}"org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:979)"
[5] = {java.lang.StackTraceElement@16682}"org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:971)"
[6] = {java.lang.StackTraceElement@16683}"com.hk.impl.dao.BaseDaoImpl.findByNamedQuery(BaseDaoImpl.java:157)"

1 个答案:

答案 0 :(得分:1)

我认为您的问题是在查询中使用'%:查询%' ..更改为:

@NamedQuery(name = "findQuesAnsByQuery"
  , query = "select f from Faq f 
        where lower(f.question) like lower(CONCAT('%', :query, '%')) 
           or lower(f.answer) like lower(CONCAT('%', :query, '%'))")

@NamedQuery(name = "findQuesAnsByQuery"
      , query = "select f from Faq f 
            where lower(f.question) like lower(:query) 
               or lower(f.answer) like lower(:query)")

然后设置参数:

.setString("query", "%" + query+ "%")