编程语言
首页 > 编程语言> > Java-在休眠中使用LIKE%

Java-在休眠中使用LIKE%

作者:互联网

如何在休眠中使用LIKE%.我想在我的hbm.xml文件中使用带有LIKE%的SQL.我有2个查询,我要合并为1个.

查询如下所示:

select * from PAY_GROUP_VW where CASE_SID=? AND CASE_TLE like %?%

我也尝试过%?%.我使用了不带%的like子句,它有效.但是%不起作用.

包装的异常:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)

这就是我传递参数的方式:

q.setParameter(0, csId);
q.setParameter(1, csTle);
return q.list();

试过%|| ?并得到以下异常:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)

解决方法:

也许user_id不是char / varchar?您必须先将带有str()的user_id转换为字符数据!

例:

SELECT *
FROM abc.def_vw a
WHERE user_id = ?
AND str(user_id) LIKE '%' || ?

标签:java,sql,spring,hibernate,hsqldb
来源: https://codeday.me/bug/20191011/1896107.html