hibernate的基础学习--多表关联数据查询

时间:2023-03-08 17:08:36

Hibernate共提供4种多表关联数据查询方式
  OID数据查询+OGN数据查询方式
  HQL数据查询方式
  QBC数据查询方式
  本地SQL查询方式(hibernate很少用)

1、OID数据查询+OGN数据查询方式

 public class TestOID_OGN {

     /**
* 什么时候时候什么时候发送
*/
@Test
public void findUser() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction();
//小李子
User user = (User) session.get(User.class, "ff80808157c82de80157c82deb3c0001");
System.out.println("========="); System.out.println(user);
ts.commit();
session.close();
}
}

2、HQL数据查询方式

/**
* HQL 数据查询
*/
public class TestHQL { @Test
public void findUser() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); String hql = "from User";
Query query = session.createQuery(hql);
@SuppressWarnings("unchecked")
List<User> users = query.list();
for (User user : users) {
System.out.println("=="+user);
} ts.commit();
session.close();
} @Test
public void findUser1() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //项目部中的所有 员工详细信息 String hql = "from Dept where deptname = '人事部'";
Query query = session.createQuery(hql);
Dept dept = (Dept) query.uniqueResult(); //OGN
Set<User> list = dept.getUsers();
for (User user : list) {
System.out.println(user);
} ts.commit();
session.close();
} @Test
public void findUser2() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //项目部中的所有 员工详细信息
//PO
//Dept dept = (Dept) session.get(Dept.class,"ff80808157c831050157c83106c40001"); //DO
Dept dept = new Dept();
dept.setDeptId("ff80808157c831050157c83106c40001"); //以对象作为条件查询:根据对象id
//只要保证该对象具备:OID PO DO String hql = "from User where dept = ? ";
Query query = session.createQuery(hql);
query.setEntity(0, dept);
//什么类型都可以只使用
//query.setParameter(0, dept); @SuppressWarnings("unchecked")
List<User> list = (List<User>) query.list();
System.out.println("=================");
for (User user : list) {
System.out.println("=="+user);
} ts.commit();
session.close();
}
}

3、QBC数据查询方式

 public class TestQBC {

     @Test
public void findUser() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("username", "小小")); User user = (User) criteria.uniqueResult();
//session.clear();
System.out.println("=========");
System.out.println(user.getDept()); ts.commit();
session.close();
} @Test
public void findUser1() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); Dept dept = new Dept();
dept.setDeptId("ff80808157c831050157c83106c40001"); Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("dept", dept)); @SuppressWarnings("unchecked")
List<User> list = criteria.list();
System.out.println("=========分割线===========");
for (User user : list) {
System.out.println(user);
} ts.commit();
session.close();
} @Test
public void findUser2() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); Dept dept = (Dept) session.get(Dept.class, "ff80808157c831050157c83106c40001"); //from User dept.deptId = '4028408157c14c210157c14c25370000'
Criteria criteria = session.createCriteria(User.class);
//使用OGN:OID
criteria.add(Restrictions.eq("dept.deptId", dept.getDeptId())); System.out.println("==========分割线============");
@SuppressWarnings("unchecked")
List<User> list = criteria.list();
for (User user : list) {
System.out.println(user);
} ts.commit();
session.close();
} @Test
public void findUser3() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); Dept dept = (Dept) session.get(Dept.class, "ff80808157c831050157c83106c40001"); //from User dept.deptName = '项目部'
Criteria criteria = session.createCriteria(User.class);
//QBC:ONG方式中如果使用的的OID可以查询,但是如果使用的是除OID之外的其它属性
//就会报异常,解决方案:createAlias("dept")
//为dept起别名
criteria.createAlias("dept", "dept");
criteria.add(Restrictions.eq("dept.deptName", dept.getDeptName())); @SuppressWarnings("unchecked")
List<User> list = criteria.list();
System.out.println("==========分割线===========");
for (User user : list) {
System.out.println(user);
} ts.commit();
session.close();
}
}

4、HQL数据查询方式(测试内连接,外连接查询)

 /**
* @author nunu
* 测试内连接,外连接查询
*/
public class TestHQL2 { /**
* 内连接:
* 有关联数据:
* 数据总量:从表被关联的数据总量
* 数据模型:主表数据(延迟从) + 从表数据(含主)
*
*/
@Test
public void findUser() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //部门:主表
//员工:从表
//sql:select d.*,u.* from t_dept d inner join t_user u on d.dept_id = u.dept_id;
String hql = "from Dept d inner join d.users";
Query query = session.createQuery(hql); @SuppressWarnings("unchecked")
List<Object[]> list = query.list();
System.out.println(list.size()); System.out.println("==========分割线===========");
for (int i = 0; i < list.size(); i++) {
Object[] objs = list.get(i);
//主表数据
Dept dept = (Dept) objs[0];
User user = (User) objs[1];
System.out.println(user);
System.out.println(dept);
/*
//主表关联数据
Set<User> userSet = dept.getUsers();
for (User user : userSet) {
//如果延迟加载:回发送新语句查询
System.out.println(user);
}
*/
System.out.println("==");
/*
//从表数据
User user = (User) objs[1]; //从表关联数据
Dept dept = user.getDept(); System.out.println(dept);
*/
} ts.commit();
session.close();
} /**
* 迫切内连接(数据重复DISTINCT)
* 数据总量:从表被关联的数据总量
* 数据模型:主表数据 (含从)
*/
@Test
public void findUser1() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //部门:主表
//员工:从表
//sql:select d.*,u.* from t_dept d inner join t_user u on d.dept_id = u.dept_id;
String hql = "from Dept d inner join fetch d.users";
Query query = session.createQuery(hql);
@SuppressWarnings("unchecked")
List<Dept> list = query.list();
System.out.println(list.size());
System.out.println("===============分隔符=================");
session.clear();
for (Dept dept : list) {
Set<User> users = dept.getUsers();
for (User user : users) {
System.out.println(user);
}
System.out.println("---------");
} ts.commit();
session.close();
} /**
* 隐式内连接
* 数据总量:主表数据总量
* 数据模型:主表数据(延迟从)
*/
@Test
public void findUser2() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //部门:主表
//员工:从表
//sql:select d.*,u.* from t_dept d inner join t_user u on d.dept_id = u.dept_id;
String hql = "from Dept"; //默认就会用户关联了
Query query = session.createQuery(hql);
@SuppressWarnings("unchecked")
List<Dept> list = query.list();
System.out.println(list.size());
//session.clear();
System.out.println("===============分隔符=================");
for (Dept dept : list) {
Set<User> users = dept.getUsers();
for (User user : users) {
System.out.println(user);
}
System.out.println("-----------------");
} ts.commit();
session.close();
} /**
左外连接: 以左侧表为准
数据总量:从表被关联数据总量+主表未关联数据总量
数据模型:主表数据(延迟从) ,从表数据 (含主)
(主表不存在关联数据的从表数据为0)
*/
@Test
public void findUser3() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //部门:主表
//员工:从表
//sql:select d.*,u.* from t_dept d inner join t_user u on d.dept_id = u.dept_id;
String hql = "from Dept d left outer join d.users";//默认就会用户关联了
Query query = session.createQuery(hql); @SuppressWarnings("unchecked")
List<Object[]> list = query.list();
System.out.println(list.size()); System.out.println("============分隔符==============");
for (int i = 0; i < list.size(); i++) {
Object[] objs = list.get(i);
//主表数据
System.out.println("\t ==主表数据");
Dept dept = (Dept) objs[0];
System.out.println("\t "+dept); System.out.println("\t ==主表关联数据");
//主表关联数据
Set<User> userSet = dept.getUsers();
for (User user : userSet) {
//如果延迟加载:回发送新语句查询
System.out.println("\t "+user);
} System.out.println("\t ==从表数据");
//从表数据
User user = (User) objs[1];
System.out.println("\t "+user); System.out.println("\t ==从表关联数据");
//从表关联数据
Dept dept1 = user.getDept(); System.out.println("\t "+dept1);
System.out.println("-------------");
}
ts.commit();
session.close();
} /**
* 迫切左外连接(数据重复DISTINCT)
数据总量:从表被关联数据总量+主表未关联数据总量
数据模型:主表数据(含从)
(主表不存在关联数据的从表数据为0)
*/
@Test
public void findUser4() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //部门:主表
//员工:从表
//sql:select d.*,u.* from t_dept d inner join t_user u on d.dept_id = u.dept_id;
String hql = "from Dept d left outer join fetch d.users";//默认就会用户关联了
Query query = session.createQuery(hql); @SuppressWarnings("unchecked")
List<Dept> list = query.list();
System.out.println("============分隔符==============");
for (Dept dept : list) {
System.out.println(dept); Set<User> userSet = dept.getUsers();
for (User user : userSet) {
System.out.println(user);
}
System.out.println("-----------");
}
ts.commit();
session.close();
} /**
* 右外连接:
数据总量:从表数据总量
数据模型:主表数据(延迟从) ,从表数据(含主)
(从表不存在关联数据的主表数据为null)
*/
@Test
public void findUser5() {
SessionFactory sf = H3Util.getSessionFactory();
Session session = sf.openSession();
Transaction ts = session.beginTransaction(); //部门:主表
//员工:从表
//sql:select d.*,u.* from t_dept d inner join t_user u on d.dept_id = u.dept_id;
String hql = "from Dept d right outer join d.users";//默认就会用户关联了
Query query = session.createQuery(hql); @SuppressWarnings("unchecked")
List<Object[]> list = query.list();
System.out.println(list.size());
System.out.println("=============分隔符=================");
for (int i = 0; i < list.size(); i++) {
Object[] objs = list.get(i);
//主表数据
//Dept dept = (Dept) objs[0];
//System.out.println(dept.getDeptName()); //主表关联数据
/*Set<User> userSet = dept.getUsers();
for (User user : userSet) {
//如果延迟加载:回发送新语句查询
System.out.println(user.getUserId());
}*/ //从表数据
User user = (User) objs[1];
System.out.println(user); //从表关联数据
// Dept dept = user.getDept();
// System.out.println(dept.getDeptName());
// System.out.println(user.getUsername());
System.out.println("-------------");
}
ts.commit();
session.close();
ts.commit();
session.close();
} }