Hibernate ORM框架——Hibernate查询之Criteria接口、Native SQL 查询、Query接口(批量操作)
一、使用双向一对多,多对一的案例
util包不变、映射文件、配置文件不变
添加了一个实体(用于setResultTransformer):
package shuang.m2oo2m; public class PersonClassInfo { private String pname; private String cname; public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } @Override public String toString() { return "PersonClassInfo [pname=" + pname + ", cname=" + cname + "]"; } }
Main测试
package com.nf; import java.util.List; import java.util.Map; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; import org.hibernate.query.NativeQuery; import org.hibernate.query.Query; import org.hibernate.transform.Transformers; import shuang.m2oo2m.Person; import shuang.m2oo2m.PersonClassInfo; import util.HibernateUtil; public class Main { public static void main(String[] args) { /*Criteria接口,单个模糊查询,组合类型模糊查询*/ queryByCriteria(); /*本地查询:即使用select语句查询*/ //nativeQuery() ; /*本地查询:自己写的select查询语句:多表查询*/ //nativeQueryTransformer(); //批量添加、删除 batchProcess(); } private static void template() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); tx.commit(); s.close(); sf.close(); } // ********************过滤条件************************* // 过滤条件值是固定的 private static void fixedWhereQuery() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "select p from Person p where p.name='p111'"; Query q = s.createQuery(hql); List<Person> list = q.list(); for (Person p : list) { System.out.println(p); } tx.commit(); s.close(); sf.close(); } //Criteria接口主要是针对查询条件进行的操作 //第一个Criteria接口案例-->like private static void queryByCriteria() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); Criteria c = s.createCriteria(Person.class); // 构造函数是受保护的,所以不能直接实例化 // Criterion guolv = new LikeExpression("name","abc"); /* * 拓展:构造函数受保护的解决方案: * (1)直接:在自己的类中写一个父类静态的方法、方便调用 * (2)间接:借用别人写好的工具类,里面写好的调用方法 * */ /*模糊查询:*/ Criterion guolv = Restrictions.like("name","p%"); c.add(guolv); /*组合类型:Restrictions.and*/ /* Criterion startWithP = Restrictions.like("name", "p%"); Criterion endWith5 = Restrictions.like("name", "%5"); Criterion guolv = Restrictions.and(startWithP,endWith5); c.add(guolv); */ List<Person> result = c.list(); for(Person p: result) { System.out.println(p); } tx.commit(); s.close(); sf.close(); } /*本地查询:即使用select语句查询*/ private static void nativeQuery() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); NativeQuery q = s.createNativeQuery("select * from persons", Person.class); List<Person> result = q.list(); for(Person p: result) { System.out.println(p); } tx.commit(); s.close(); sf.close(); } /*本地查询:自己写的select查询语句:多表查询*/ private static void nativeQueryTransformer() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); NativeQuery q = s .createNativeQuery("select persons.name as pname,CLASSINFO.name as cname from persons inner join classInfo on persons.rci = CLASSINFO.cid"); q.setResultTransformer(Transformers.aliasToBean(PersonClassInfo.class));//为的是知道是哪个类 List<PersonClassInfo> result = q.list(); for(PersonClassInfo p: result) { System.out.println(p); } tx.commit(); s.close(); sf.close(); } /*批量添加删除 * batch批量的意思 * Hibernate 对update,delete操作支持比较好 * 而且hql的update,delete主要用来批量处理的 * 但是对insert操作,支持就有限制 * 不支持insert into xxx(id,name) values(1,'sss') * 但其支持的是insert into xxx(id,name) select */ private static void batchProcess() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "update Person p set p.name = p.name + 'abc' "; Query q = s.createQuery(hql); q.executeUpdate(); tx.commit(); s.close(); sf.close(); } }