HQL的内连接查询
对于HQL内链接查询,查询的是两张表的数据,这两张表的数据首先是保存在数组之中,然后在将每一个数组保存在List集合之中进行返回
代码片段:
1 @TestView Code
2 // 内连接
3 public void fun1() {
4 SessionFactory factory = null;
5 Session session = null;
6 Transaction tx = null;
7 try {
8 factory = Tools.getSessionFactory();
9 session = factory.openSession();
10 tx = session.beginTransaction();
11 // mans是Customer实体类的关联LinkMan实体类的一个集合属性
12 Query query = session.createQuery("from Customer c inner join c.mans");
13 List list = query.list();
14 tx.commit();
15 } catch (Exception e) {
16 tx.rollback();
17 e.printStackTrace();
18 } finally {
19 session.close();
20 }
21 }
debug截图:
HQL迫切内连接查询
hql的迫切内连接查询,只需要将内连接查询加上一个fetch就行,如下:
1 from Customer c inner join fetch c.mans
迫切内连接和内连接的主要的区别是,内连接的List每部分是一个对象数组,而迫切内连接的List每部分返回的是一个对象。
代码片段:
1 @TestView Code
2 // 迫切内连接
3 public void fun2() {
4 SessionFactory factory = null;
5 Session session = null;
6 Transaction tx = null;
7 try {
8 factory = Tools.getSessionFactory();
9 session = factory.openSession();
10 tx = session.beginTransaction();
11 // mans是Customer实体类的关联LinkMan实体类的一个集合属性
12 Query query = session.createQuery("from Customer c inner join fetch c.mans");
13 List list = query.list();
14 tx.commit();
15 } catch (Exception e) {
16 tx.rollback();
17 e.printStackTrace();
18 } finally {
19 session.close();
20 }
21 }
Debug截图:
HQL的左外连接
左外连接的hql语句:
1 from Customer c left outer join 关联另一个实体的属性
代码片段:
1 @TestView Code
2 // 左外连接
3 public void fun3() {
4 SessionFactory factory = null;
5 Session session = null;
6 Transaction tx = null;
7 try {
8 factory = Tools.getSessionFactory();
9 session = factory.openSession();
10 tx = session.beginTransaction();
11 Query query = session.createQuery("from Customer c left outer join c.mans");
12 List list = query.list();
13 tx.commit();
14 } catch (Exception e) {
15 tx.rollback();
16 e.printStackTrace();
17 } finally {
18 session.close();
19 }
20 }
Debug截图:
HQL的左外迫切连接
迫切左外连接hql语句:
from Customer c left outer join fetch 关联的另一个实体类在此类中的属性
代码片段:
1 @TestView Code
2 // 迫切左外连接
3 public void fun4() {
4 SessionFactory factory = null;
5 Session session = null;
6 Transaction tx = null;
7 try {
8 factory = Tools.getSessionFactory();
9 session = factory.openSession();
10 tx = session.beginTransaction();
11 Query query = session.createQuery("from Customer c left outer join fetch c.mans");
12 List list = query.list();
13 tx.commit();
14 } catch (Exception e) {
15 tx.rollback();
16 e.printStackTrace();
17 } finally {
18 session.close();
19 }
20 }
Debug截图: