Java如何使用Hibernate的Restriction.in标准?
此示例演示了HibernateRestriction.in准则的使用。此限制将基于为bean的特定属性定义的参数集合来查询某些记录。
package org.nhooo.example.hibernate.criteria;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.nhooo.example.hibernate.model.Genre;
import java.util.List;
public class RestrictionInDemo {
public static Session getSession() throws HibernateException {
String cfg = "hibernate.cfg.xml";
SessionFactory sessionFactory = new Configuration().configure(cfg)
.buildSessionFactory();
return sessionFactory.openSession();
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
try (Session session = getSession()) {
//创建一个Criteria并为该属性添加一个in约束
//ID。受限制的类型将返回ID为1、2、3的流派
//和4。
Criteria criteria = session.createCriteria(Genre.class)
.add(Restrictions.in("id", 1L, 2L, 3L, 4L));
List<Genre> result = criteria.list();
for (Genre genre : result) {
System.out.println(genre);
}
}
}
}
热门推荐
2 合字的祝福语简短
10 启程出发路途祝福语简短
11 新春进门祝福语大全简短
12 六一婚礼文案祝福语简短
13 18岁简短祝福语经典
14 男朋友祝福语简短新年
15 送个姐妹生日祝福语简短
16 老师对学校祝福语简短
17 新房开工吉日祝福语简短
18 周年蛋糕祝福语简短英语