我有一个像这样的表:
Users (user_id, name, age, country_id)
现在我想要一个如下查询:
SELECT *
FROM users
where country_id in (1,2,3,4,5)
用户没有任何关系,我只想在没有任何关联的情况下执行此操作。
我想通过条件查询来执行此操作。
我看到了限制,但不确定它是否有 where in 子句:
sessionFactory.getCurrentSession().createCriteria(User.class)
.add(Restrictions.eq("country_id", countryId)
.list();
所以我的更改是我有一个需要传递的国家/地区 ID 列表(List CountryIds)。
最佳答案
您想要“country_id in (...)”吗?
如果是,那么:
sessionFactory.getCurrentSession().createCriteria(User.class)
.add(Restrictions.in("countryId", countryIds)
.list();
我还使用了“countryId” - 因为在标准中您使用属性名称,而不是表列名称。
关于java - Hibernate where 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10066703/