通过使用like运算符进行Hibernate HQL查询 [英] Hibernate HQL query by using like operator
本文介绍了通过使用like运算符进行Hibernate HQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请执行以下映射
@Entity
public class User {
private Integer id;
@Id;
private Integer getId() {
return this.id;
}
}
通知ID是一个整数.现在,我需要使用like运算符来进行此HQL查询
Notice id is an Integer. Now i need this HQL query by using like operator
Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.id like :userId");
ATT:它很像 运算符,而不是 = (等于运算符)
ATT: IT IS like operator NOT = (equals operator)
然后我用
List<User> userList = query.setParameter("userId", userId + "%").list();
但是不起作用,因为Hibernate抱怨在调用User.id的getter时发生了IllegalArgumentException.
But does not work because Hibernate complains IllegalArgumentException occured calling getter of User.id
即使我使用
query.setString("userId", userId + "%");
它不起作用
我应该使用什么来传递查询?
What should i use to pass the query ?
推荐答案
根据Hibernate参考:
According to Hibernate reference:
str()用于将数字或时间值转换为可读的字符串
str() is used for converting numeric or temporal values to a readable string
所以当我使用
from User u where str(u.id) like :userId
效果很好
这篇关于通过使用like运算符进行Hibernate HQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文