http://1194867672-qq-com.iteye.com/blog/1522906
publicclass TestHql {
/** 查询全部 (推荐) */
@Test
publicvoid test_list() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp";
Query query = session.createQuery(hql);
query.setCacheable(true); // 设置缓存
List<Emp> list = query.list();
show(list);
session.close();
}
/** 查询全部 */
@Test
publicvoid test_iterator() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp";
Query query = session.createQuery(hql);
Iterator<Emp> it = query.iterate();
while (it.hasNext()) {
Emp emp = it.next();
System.out.println(emp);
}
session.close();
}
/** 分页查询 */
// select * from
// ( select e.*, rownum rn from ( select * from emp ) e where rownum <= 10 )
// where rn > 1
@Test
publicvoid test_pagination() {
int pageNo = 1; // 当前页数
int pageSize = 5; // 每页显示条数
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp";
Query query = session.createQuery(hql);
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
query.setCacheable(true); // 设置缓存
List<Emp> list = query.list();
show(list);
session.close();
}
/** 查询结果是唯一值 */
@Test
publicvoid test_uniqueResult() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp where empno=7369";
Query query = session.createQuery(hql);
Emp emp = (Emp) query.uniqueResult();
System.out.println(emp);
session.close();
}
/** 投影查询 */
@Test
publicvoid test_projection1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select empno from Emp"; // 查询单值
Query query = session.createQuery(hql);
List<Integer> list = query.list();
for (Integer empno : list) {
System.out.println(empno);
}
session.close();
}
/** 投影查询 */
@Test
publicvoid test_projection2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select empno,ename,hiredate,sal from Emp"; // 查询多值
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] arr : list) {
System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3]);
}
session.close();
}
/** 投影查询 */
@Test
publicvoid test_projection3() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select new Emp(empno,ename,hiredate,sal) from Emp"; // 查询多值 封装在一个对象中
Query query = session.createQuery(hql);
List<Emp> list = query.list();
for (Emp emp : list) {
System.out.println(emp.getEmpno() + "\t" + emp.getEname() + "\t" + emp.getHiredate() + "\t" + emp.getSal());
}
session.close();
}
/** 投影查询 */
@Test
publicvoid test_projection4() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select new Map(empno,ename,hiredate,sal) from Emp"; // map 大小都都可以, 查询多值 装在一个Map中
Query query = session.createQuery(hql);
List<Map> list = query.list();
for (Map map : list) {
System.out.println(map.get("0") + "\t" + map.get("1") + "\t" + map.get("2") + "\t" + map.get("3"));
}
session.close();
}
/** 投影查询 (使用别名) */
@Test
publicvoid test_projection5() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select new Map(e.empno as empno ,e.ename as ename ,e.hiredate as hiredate ,e.sal as sal) from Emp as e";
Query query = session.createQuery(hql);
List<Map> list = query.list();
for (Map map : list) {
System.out.println(map.get("empno") + "\t" + map.get("ename") + "\t" + map.get("hiredate") + "\t" + map.ge("sal"));
}
session.close();
}
/** 去掉重复值 */
@Test
publicvoid test_distinct() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select distinct job from Emp";
Query query = session.createQuery(hql);
List<String> list = query.list();
for (String job : list) {
System.out.println(job);
}
session.close();
}
/** 条件查询 */
@Test
publicvoid test_where1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
// String hql = "from Emp where empno=7369 and ename='SMITH'";
// String hql = "from Emp where empno=7369 or ename='ALLEN'";
// String hql = "from Emp where not (ename='SMITH')"; // () 可以不写
// String hql = "from Emp where sal between 3000 and 5000";
// String hql = "from Emp where sal not between 3000 and 5000";
// String hql = "from Emp where empno in(7369,7900,7654)";
// String hql = "from Emp where empno not in(7369,7900,7654)";
// String hql = "from Emp where ename like 'S%'";
// String hql = "from Emp where ename not like 'S%'";
// String hql="from Emp where comm is null";
// String hql="from Emp where comm is not null";
// String hql = "from Emp where sal >= 3000 and sal<= 5000)";
// String hql="from Emp where sal <>3000";
String hql = "from Emp where sal !=3000";
Query query = session.createQuery(hql);
List<Emp> list = query.list();
show(list);
session.close();
}
/** 条件查询 */
@Test
publicvoid test_where2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp where ename like ?";
Query query = session.createQuery(hql);
query.setString(0, "J%");
// String hql = "from Emp where ename like :ename";
// Query query = session.createQuery(hql);
// query.setString("ename", "J%");
List<Emp> list = query.list();
show(list);
session.close();
}
/** 统计函数 */
@Test
publicvoid test_statistics1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select count(*),min(sal),max(sal),avg(sal),sum(sal) from Emp";
Query query = session.createQuery(hql);
Object[] values = (Object[]) query.uniqueResult();
System.out.println("总数:" + values[0]);
System.out.println("最小值:" + values[1]);
System.out.println("最大值:" + values[2]);
System.out.println("平均值:" + values[3]);
System.out.println("合计:" + values[4]);
session.close();
}
/** 统计函数 */
@Test
publicvoid test_statistics2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select deptno,avg(sal) from Emp group by deptno having avg(sal)>2000 order by deptno";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] arr : list) {
System.out.println("部门编号:" + arr[0] + "\t" + "平均薪资:" + arr[1]);
}
session.close();
}
/** 使用函数(和底层数据库有关) */
@Test
publicvoid test_function() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select to_char(hiredate,'yyyy-mm-dd') from Emp";
Query query = session.createQuery(hql);
List<String> list = query.list();
for (String ename : list) {
System.out.println(ename);
}
session.close();
}
/** 排序 */
@Test
publicvoid test_orderby() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp order by job,sal desc";
Query query = session.createQuery(hql);
List<Emp> list = query.list();
show(list);
session.close();
}
// 遍历集合
privatevoid show(List<Emp> list) {
for (Emp emp : list) {
System.out.println(emp);
}
}
}
publicclass TestQBC {
/** 查询全部 */
@Test
publicvoid test_list() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.setCacheable(true); // 设置缓存
List<Emp> list = criteria.list();
show(list);
session.close();
}
/** 查询全部 */
@Test
publicvoid test_iterator() {
// 没有iterator
}
/** 分页查询 */
// select * from
// ( select e.*, rownum rn from ( select * from emp ) e where rownum <= 10 )
// where rn > 1
@Test
publicvoid test_pagination() {
int pageNo = 1; // 当前页数
int pageSize = 5; // 每页显示条数
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.setFirstResult((pageNo - 1) * pageSize);
criteria.setMaxResults(pageSize);
criteria.setCacheable(true); // 设置缓存
List<Emp> list = criteria.list();
show(list);
session.close();
}
/** 查询结果是唯一值 */
@Test
publicvoid test_uniqueResult() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.add(Restrictions.eq("empno", 7369));
Emp emp = (Emp) criteria.uniqueResult();
System.out.println(emp);
session.close();
}
/** 投影查询 */
@Test
publicvoid test_projection1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.setProjection(Projections.property("empno"));
List<Integer> list = criteria.list();
for (Integer empno : list) {
System.out.println(empno);
}
session.close();
}
/** 投影查询 */
@Test
publicvoid test_projection2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("empno"));
projectionList.add(Projections.property("ename"));
projectionList.add(Projections.property("hiredate"));
projectionList.add(Projections.property("sal"));
criteria.setProjection(projectionList);
List<Object[]> list = criteria.list();
for (Object[] arr : list) {
System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3]);
}
session.close();
}
/** 去掉重复值 */
@Test
publicvoid test_distinct() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.setProjection(Projections.distinct(Projections.property("job")));
List<String> list = criteria.list();
for (String job : list) {
System.out.println(job);
}
session.close();
}
/** 条件查询 */
@Test
publicvoid test_where() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
// criteria.add(Restrictions.and(Restrictions.eq("empno", 7369), Restrictions.eq("ename", "SMITH")));
// criteria.add(Restrictions.or(Restrictions.eq("empno", 7369), Restrictions.eq("ename", "ALLEN")));
// criteria.add(Restrictions.not(Restrictions.eq("ename", "SMITH")));
// criteria.add(Restrictions.between("sal", 3000.0, 5000.0));
// criteria.add(Restrictions.not(Restrictions.between("sal", 3000.0, 5000.0)));
// criteria.add(Restrictions.in("empno", new Object[] { 7369, 7900, 7654 }));
// criteria.add(Restrictions.not(Restrictions.in("empno", new Object[] { 7369, 7900, 7654 })));
// criteria.add(Restrictions.like("ename", "S%", MatchMode.START));
// criteria.add(Restrictions.not(Restrictions.like("ename", "S%", MatchMode.START)));
// criteria.add(Restrictions.isNull("comm"));
// criteria.add(Restrictions.not(Restrictions.isNull("comm")));
// criteria.add(Restrictions.and(Restrictions.ge("sal", 3000.0), Restrictions.le("sal", 5000.0)));
criteria.add(Restrictions.ne("sal", 3000.0));
List<Emp> list = criteria.list();
show(list);
session.close();
}
/** 统计函数 */
@Test
publicvoid test_statistics1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.rowCount());
projectionList.add(Projections.min("sal"));
projectionList.add(Projections.max("sal"));
projectionList.add(Projections.avg("sal"));
projectionList.add(Projections.sum("sal"));
criteria.setProjection(projectionList);
Object[] values = (Object[]) criteria.uniqueResult();
System.out.println("总数:" + values[0]);
System.out.println("最小值:" + values[1]);
System.out.println("最大值:" + values[2]);
System.out.println("平均值:" + values[3]);
System.out.println("合计:" + values[4]);
session.close();
}
/** 统计函数 */
// select deptno,avg(sal) from Emp group by deptno having avg(sal)>2000 order by deptno;
@Test
publicvoid test_statistics2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("deptno"));
projectionList.add(Projections.avg("sal"));
projectionList.add(Projections.groupProperty("deptno"));
criteria.setProjection(projectionList);
// TODO having 怎么表示 暂时没有找到
criteria.addOrder(Order.asc("deptno"));
List<Object[]> list = criteria.list();
for (Object[] arr : list) {
System.out.println("部门编号:" + arr[0] + "\t" + "平均薪资:" + arr[1]);
}
session.close();
}
/** 使用函数(和底层数据库有关) */
@Test
publicvoid test_function() {
// TODO
}
/** 排序 */
@Test
publicvoid test_orderby() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.addOrder(Order.asc("job"));
criteria.addOrder(Order.desc("sal"));
List<Emp> list = criteria.list();
show(list);
session.close();
}
// 遍历集合
privatevoid show(List<Emp> list) {
for (Emp emp : list) {
System.out.println(emp);
}
}
}
publicclass TestQBE {
/** QBE查询 适合条件检索 */
@Test
publicvoid test_qbe() {
Emp emp = new Emp();
emp.setDeptno(10);
emp.setJob("MANAGER");
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Emp.class);
criteria.add(Example.create(emp));// 注意
List<Emp> list = criteria.list();
show(list);
session.close();
}
// 遍历集合
privatevoid show(List<Emp> list) {
for (Emp emp : list) {
System.out.println(emp);
}
}
}
publicclass TestSql {
/** 查询全部 */
@Test
publicvoid test_sql1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String sql = "select * from emp";
SQLQuery query = session.createSQLQuery(sql);
List<Object[]> list = query.list();
for (Object[] arr : list) {
System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3] + "\t" + arr[4] + "\t" + arr[5] + "\t" + arr[6] + "\t" + arr[7]);
}
session.close();
}
/** 查询全部 */
@Test
publicvoid test_sql2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String sql = "select * from emp";
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("empno", StandardBasicTypes.INTEGER);
query.addScalar("ename", StandardBasicTypes.STRING);
query.addScalar("job", StandardBasicTypes.STRING);
query.addScalar("mgr", StandardBasicTypes.INTEGER);
query.addScalar("hiredate", StandardBasicTypes.DATE);
query.addScalar("sal", StandardBasicTypes.DOUBLE);
query.addScalar("comm", StandardBasicTypes.DOUBLE);
query.addScalar("deptno", StandardBasicTypes.INTEGER);
List<Object[]> list = query.list();
for (Object[] arr : list) {
System.out.println(arr[0] + "\t" + arr[1] + "\t" + arr[2] + "\t" + arr[3] + "\t" + arr[4] + "\t" + arr[5] + "\t" + arr[6] + "\t" + arr[7]);
}
session.close();
}
/** 查询全部 */
@Test
publicvoid test_sql3() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String sql = "select * from emp";
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("empno", StandardBasicTypes.INTEGER);
query.addScalar("ename", StandardBasicTypes.STRING);
query.addScalar("job", StandardBasicTypes.STRING);
query.addScalar("mgr", StandardBasicTypes.INTEGER);
query.addScalar("hiredate", StandardBasicTypes.DATE);
query.addScalar("sal", StandardBasicTypes.DOUBLE);
query.addScalar("comm", StandardBasicTypes.DOUBLE);
query.addScalar("deptno", StandardBasicTypes.INTEGER);
query.setResultTransformer(Transformers.aliasToBean(Emp.class));
List<Emp> list = query.list();
show(list);
session.close();
}
/** 查询全部 (推荐) */
@Test
publicvoid test_sql4() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String sql = "select * from emp";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Emp.class);
// String sql = "select {e.*} from emp e";
// SQLQuery query = session.createSQLQuery(sql);
// query.addEntity("e", Emp.class);
query.setCacheable(true); // 设置缓存
List<Emp> list = query.list();
show(list);
session.close();
}
/** 条件查询 */
@Test
publicvoid test_where() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String sql = "select * from emp where ename like ?";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Emp.class);
query.setString(0, "J%");
// String sql = "select * from emp where ename like :ename";
// SQLQuery query = session.createSQLQuery(sql);
// query.addEntity(Emp.class);
// query.setString("ename", "J%");
List<Emp> list = query.list();
show(list);
session.close();
}
// 遍历集合
privatevoid show(List<Emp> list) {
for (Emp emp : list) {
System.out.println(emp);
}
}
}
publicclass TestCRUD {
// 添加
@Test
publicvoid test_save() {
Customer customer = new Customer();
customer.setName("monday");
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Serializable id = session.save(customer); // 返回OID
tx.commit();
session.close();
System.out.println("id=" + id);
}
// 添加
@Test
publicvoid test_persist() {
Customer customer = new Customer();
customer.setName("monday");
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.persist(customer); // 无返回值
tx.commit();
session.close();
}
// 添加
@Test
publicvoid test_saveOrUpdate1() {
Customer customer = new Customer();
customer.setName("monday");
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(customer); // OID 为空 执行save操作
tx.commit();
session.close();
}
// 修改
@Test
publicvoid test_saveOrUpdate2() {
Customer customer = new Customer();
customer.setName("monday_update");
customer.setId(5);
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(customer); // OID 为不空 执行update操作
tx.commit();
session.close();
}
// 修改
@Test
publicvoid test_update() {
Customer customer = new Customer();
customer.setName("monday_update2");
customer.setId(5);
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.update(customer);
tx.commit();
session.close();
}
// 修改
@Test
publicvoid test_update_hql() {
String hql = "update Customer set name=? where id=?";
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(hql);
query.setParameter(0, "springfuncs");
query.setParameter(1, 5);
query.executeUpdate();
tx.commit();
session.close();
}
// 删除
@Test
publicvoid test_delete() {
Customer customer = new Customer();
customer.setId(5);
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.delete(customer);
tx.commit();
session.close();
}
// 删除
@Test
publicvoid test_delete_hql() {
String hql = "delete from Customer where id=?";
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(hql);
query.setParameter(0, 6);
query.executeUpdate();
tx.commit();
session.close();
}
// 查询
@Test
publicvoid test_get() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
// Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 1);
// tx.commit();
session.close();
System.out.println(customer);
}
// 查询
@Test
publicvoid test_load() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
// Transaction tx = session.getTransaction();
// tx.begin();
Customer customer = (Customer) session.load(Customer.class, 1);
// tx.commit();
Hibernate.initialize(customer);
session.close();
System.out.println(customer);
// 或者
// System.out.println(customer);
// session.close();
}
}
publicclass TestAssociated {
// 交叉连接(多表的笛卡儿积不常用)
// select c.*,o.* from customers c, orders o
@Test
publicvoid test_cross_join() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Customer c , Order o";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (int i = 0; i < list.size(); i++) {
Object[] obj = (Object[]) list.get(i);
Customer customer = (Customer) obj[0];
Order order = (Order) obj[1];
System.out.println((i + 1) + "---" + customer);
System.out.println((i + 1) + "---" + order);
}
session.close();
}
// 内链接
// select c.* from customers c inner join orders o on c.id=o.customer_id
// select o.* from orders o where o.customer_id=?
@Test
publicvoid test_inner_join() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select c from Customer c inner join c.orders"; // inner 可省略
Query query = session.createQuery(hql);
List<Customer> list = query.list();
showCustomer(list);
session.close();
}
// 左外连接
// select c.* from customers c left outer join orders o on c.id=o.customer_id
// select o.* from orders o where o.customer_id=?
@Test
publicvoid test_left_outer_join() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select c from Customer c left outer join c.orders"; // outer 可省略
Query query = session.createQuery(hql);
List<Customer> list = query.list();
showCustomer(list);
session.close();
}
// 右连接
// select c.*, o.* from customers c right outer join orders o on c.id=o.customer_id
@Test
publicvoid test_right_outer_join() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Customer c right outer join c.orders"; // outer 可省略
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (int i = 0; list != null && i < list.size(); i++) {
Object[] obj = list.get(i);
Customer customer = (Customer) obj[0];
Order order = (Order) obj[1];
System.out.println((i + 1) + "---" + customer);
System.out.println((i + 1) + "---" + order);
}
session.close();
}
// 迫切内连接(推荐)
// select c.*,o.* from customers c inner join orders o on c.id=o.customer_id
@Test
publicvoid test_inner_join_fetch() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select c from Customer c inner join fetch c.orders"; // inner 可省略
Query query = session.createQuery(hql);
query.setCacheable(true); // 设置缓存
List<Customer> list = query.list();
showCustomer(list);
session.close();
}
// 迫切左外连接(推荐)
// select c.*,o.* from customers c left outer join orders o on c.id=o.customer_id
@Test
publicvoid test_left_outer_join_fetch() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "select c from Customer c left outer join fetch c.orders"; // outer可省略
Query query = session.createQuery(hql);
query.setCacheable(true); // 设置缓存
List<Customer> list = query.list();
showCustomer(list);
session.close();
}
// 迫切左外连接(推荐)
// select c.*, o.* from customers c left outer join orders o on c.id=o.customer_id
@Test
publicvoid test_left_outer_join_fetch_QBC() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Customer.class).setFetchMode("orders", FetchMode.JOIN);
criteria.setCacheable(true); // 设置缓存
List<Customer> list = criteria.list();
showCustomer(list);
session.close();
}
// 打印Order信息
privatevoid showCustomer(List<Customer> list) {
for (Customer customer : list) {
System.out.println(customer);
if (customer.getOrders() != null && customer.getOrders().size() > 0) {
Set<Order> orders = customer.getOrders();
for (Iterator<Order> it = orders.iterator(); it.hasNext();) {
Order order = it.next();
System.out.println(order);
}
}
}
}
}
publicclass TestCache {
// 测试Session缓存
@Test
publicvoid test_cache1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Emp emp1 = (Emp) session.get(Emp.class, 7369);
Emp emp2 = (Emp) session.get(Emp.class, 7369);
System.out.println(emp1 == emp2);
}
// 测试 EHCache缓存
// 配置缓存的话,会执行一条SQL,否则是2条SQL
@Test
publicvoid test_cache2() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from Emp";
Query query = session.createQuery(hql);
query.setCacheable(true);
query.list();
query = session.createQuery(hql);
query.setCacheable(true);
query.list();
}
}