请查看以下映射
@Entity
public class User {
private Integer id;
@Id;
private Integer getId() {
return this.id;
}
}
注意 id 是一个整数。现在我需要使用 like 运算符来构建这个 HQL 查询。
Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.id like :userId");
注意:这里使用的是like操作符,而不是=(等于操作符)。
然后我使用
List<User> userList = query.setParameter("userId", userId + "%").list();
但是不起作用,因为Hibernate抱怨调用User.id的getter方法时出现了IllegalArgumentException异常。
即使我使用
query.setString("userId", userId + "%");
它不起作用。
我应该使用什么来传递查询?
where userId > 22100 and userId < 22200
。根据您的数据库引擎和表大小,差异可能会非常显着(因为此处将使用索引,但不会用于LIKE)。 - ChssPly76