Hibernate之HQL多表查询

xiaoxiao2025-10-14  5

多表的查询进行使用HQL语句进行查询,HQL语句和SQL语句的查询语法比较类似

内连接查询 显示内连接 select * from customer c inner join orders o on c.cid = o.cno 隐式内连接 select * from customers c.orders o where c.cid = o.cno; 外连接查询 左连接查询 select * from customers c left join orders o on c.cid = o.cno 右连接查询 select * from customer c right join orders o on c.cid = o.cno

HQL 的多表查询

迫切和非迫切 非迫切返回结果是Object[]迫切连接返回的结果是对象,把客户的信息封装到客户的对象中,把订单信息封装到客户的set集合中。

内连接查询

内连接使用 inner join,默认返回的是Object数组 Session session = HibernateUtils.getCurrentSession(); Transaction tr = session.beginTransaction(); List<Customer> list = session.beginTransaction(); Set<Customer> set = new HashSet<Customer>(list); for(Customer customer:set){ System.out.println(customer); } tr.commit();

迫切内连接 :inner join fetch,返回的是实体对象

Session session = HibernateUtils.getCurrentSession(); Transaction tr = session.beginTransaction(); List<Customer> list = session.createQuery("from Customer c inner join fetch c.linkmans").list(); Set<Customer> set = new HashSet<Customer>(list);//去掉重复 for(Customer customer : set){ System.out.println(customer); } tr.commit;

左外连接查询

左外连接:封装成List<Object[]>

迫切左外连接

Session session = HIbernateUtils.getCurrentSession(); Transaction tr = session.beginTransaction(); List<Customer> list = session.createQuery("from Customer c left join fetch c.linkmans").list(); for(Customer customer : set){ System.out.println(customer); } tr.commit();
转载请注明原文地址: https://www.6miu.com/read-5037915.html

最新回复(0)