HQL之多表查询(一对多和多对多)
一、一对多
//内链接,两种方式效果一样,查询的是两边都有的数据
SELECT c.cname,s.sname FROM classes c INNER JOIN student s ON s.cid=c.cid;
//左外连接,在内链接基础上,左边表有而右边表没有,两种方式等效;
SELECT c.* ,s.* FROM student s LEFT OUTER JOIN classes c ON s.cid=c.cid;
SELECT c.* ,s.* FROM student s LEFT JOIN classes c ON s.cid=c.cid;
//右外连接,在内链接基础上,右边有而左边无,两种方式等效;
SELECT c.* ,s.* FROM classes c RIGHT OUTER JOIN student s ON s.cid=c.cid;
SELECT c.* ,s.* FROM classes c RIGHT JOIN student s ON s.cid=c.cid;
/**
* 1.一对多
* sql:select c.*,s.* from classes c,student s where c.cid=s.cid;
* hql:from Classes c,Student s where c.cid=s.classes.cid,注意与上句的区别;
* 得到的list是object[],数组中的元素是Classes和Student对象;
*
*/
@Test
public void testOneToMany_EQ(){
Session session = sessionFactory.openSession();
Query query = session.createQuery( "from Classes c,Student s where c.cid=s.classes.cid");
List list = query.list();
System. out.println(query.list().size());
session.close();
}
/**
* 2.带属性的查询;
* list中装的是object[];
*/
@Test
public void testOneToMany_EQ_Property(){
Session session = sessionFactory.openSession();
Query query = session.createQuery( "select c.cname,s.sname from Classes c,Student s where c.cid=s.classes.cid");
query.list();
session.close();
}
/**
* 3.带属性查询,将查询结果封装成一个bean;
* 得到的list中装的是classView对象;
*/
@Test
public void testOneToMany_EQ_Property_Constructor(){
Session session = sessionFactory.openSession();
Query query = session.createQuery( "select new cn.itheima03.hibernate.domain.ClassesView(c.cname,s.sname) " +
"from Classes c,Student s where c.cid=s.classes.cid");
List list = query.list();
session.close();
}
/**
* 4.内连接
* 结果与例子1一样;
*/
@Test
public void testOneToMany_InnerJoin_Query(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Classes c inner join c.students s");
Query query = session.createQuery(buffer.toString());
query.list();
session.close();
}
/**
* 5.迫切内连接1:获取所有有学生的班级及班级下的学生;
* 要想得到的集合中装的Classes对象,对象中set集合中装student,可以使用迫切内链接。
*
*/
@Test
public void testOneToMany_InnerJoin_Fetch_Query_1(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Classes c inner join fetch c.students s");
Query query = session.createQuery(buffer.toString());
List list = query.list();
session.close();
}
/**
* 6.迫切内连接2
* 从学生端出发;
*/
@Test
public void testOneToMany_InnerJoin_Fetch_Query_2(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Student s inner join fetch s.classes c");
Query query = session.createQuery(buffer.toString());
query.list();
session.close();
}
/**
* 7.迫切内连接3:获取属性,封装结果;
* select new cn.itheima03.hibernate.domain.ClassView(c.cname,s.sname)
* from Student s inner join fetch s.classes c;
* 上述的 hql语句会报错,因为from后面想要的结构和select想要的结构是冲突的,所以
如果在from后面加fetch,不能写select语句,如果加select,不能写fetch,两者只能选择其一
*
*/
@Test
public void testOneToMany_InnerJoin_Fetch_Query_Property(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
//下面的写法不对;
// buffer.append("select new cn.itheima03.hibernate.domain.ClassView(c.cname,s.sname) " +
// " from Student s inner join fetch s.classes c");
//不要fetch;
buffer.append( "select new cn.itheima03.hibernate.domain.ClassesView(c.cname,s.sname) " +
" from Student s inner join s.classes c " );
Query query = session.createQuery(buffer.toString());
List list = query.list();
session.close();
}
/**
* 8.迫切左外连接
* 从班级出发,得到班级对应的学生
*/
@Test
public void testOneToMany_LeftJoin_Fetch(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Classes c left outer join fetch c.students s");
Query query = session.createQuery(buffer.toString());
List<Classes> list = query.list();
for (Classes classes : list) {
System. out.println("classes:" +classes.getCname());
Set<Student> students = classes.getStudents();
for (Student student : students) {
System. out.println(" student:" +student.getSname());
}
}
session.close();
}
/**
* 9.迫切左外连接2
* 从学生出发,得到对应的班级
*/
@Test
public void testOneToMany_RightJoin_Fetch(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Student s left outer join fetch s.classes ");
Query query = session.createQuery(buffer.toString());
List<Student> list = query.list();
for (Student student : list) {
System. out.println("student:" +student.getSname());
if (student.getClasses()!=null) {
System. out.println(" " +student.getClasses().getCname());
}
}
session.close();
}
|
/**
* 1.得到所有的学生以及其对应的课程
* 从学生端出发
* list装的是学生;
*/
@Test
public void testManyToMany_LeftJoin_Fecth(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Student s left outer join fetch s.courses");
Query query = session.createQuery(buffer.toString());
List list = query.list();
session.close();
}
/**
* 2.得到所有的课程及课程下对应的学生;
* list装的是课程
*/
@Test
public void testManyToMany_LeftJoin_Fecth_2(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Course c left outer join fetch c.students s");
Query query = session.createQuery(buffer.toString());
query.list();
session.close();
}
/**
* 3.一对多和多对多的结合
* 得到所有班级下的所有学生以及所有学生下的所有课程;
* 从班级出发
*/
@Test
public void testManyToManyAndOneToMany(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Classes c left outer join fetch" +
" c.students s left outer join fetch s.courses");
Query query = session.createQuery(buffer.toString());
List<Classes> classeList = query.list();
//去掉集合中的重复元素
Set<Classes> sets = new HashSet<Classes>(classeList);
classeList = new ArrayList<Classes>(sets);
System. out.println(classeList.size());
for(Classes classes:classeList){//遍历班级
System. out.println(classes.getCname());
Set<Student> students = classes.getStudents();//得到班级下的学生
for(Student student:students){//遍历学生
System. out.println(student.getSname());
Set<Course> courses = student.getCourses();
for(Course course:courses){//遍历学生下的课程
System. out.println(course.getCname());
}
}
}
session.close();
}
/**
* 从中间表出发,班级有学生,学生修课程,故从学生角度出发进行查询;
*/
@Test
public void testManyToManyAndOneToMany_2(){
Session session = sessionFactory.openSession();
StringBuffer buffer = new StringBuffer();
buffer.append( "from Student s left outer join fetch s.classes c
left outer join fetch s.courses cc");
Query query = session.createQuery(buffer.toString());
List<Student> studentList = query.list();
for(Student student:studentList){
System. out.println(student.getSname());
Classes classes = student.getClasses();
System. out.println(classes.getCname());
Set<Course> courses = student.getCourses();
for(Course course:courses){
System. out.println(course.getCname());
}
}
session.close();
}
/*******************************************************************************/
/**
* 面向对象的查询
*/
@Test
public void testQueryCriteria(){
Session session = sessionFactory.openSession();
List<Classes> classesList = session.createCriteria(Classes.class).list() ;
System. out.println(classesList.size());
session.close();
}
@Test
public void testQueryCriteria_Where(){
Session session = sessionFactory.openSession();
Classes classes = (Classes)session.createCriteria(Classes.class).add(Restrictions.eq("cid" , 1L)).uniqueResult();
System. out.println(classes.getCname());
session.close();
}
}
|
HQL之多表查询(一对多和多对多)的更多相关文章
- django(3) 一对多跨表查询、ajax、多对多
1.一对多跨表查询获取数据的三种形式:对象.字典.元组 例:有host与business两张表,host与business的id字段关联,business在host表中的对象名是b, 通过查询hos ...
- django ORM模型表的一对多、多对多关系、万能双下划线查询
一.外键使用 在 MySQL 中,如果使用InnoDB引擎,则支持外键约束.(另一种常用的MyIsam引擎不支持外键) 定义外键的语法为fieldname=models.ForeignKey(to_c ...
- Mybatis_resultMap的关联方式实现多表查询(一对多)
a)在 ClazzMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应学生的信息. b)通过<resultMap>定义映射关系, 并通过<c ...
- 2.1、Hibernate多表操作--一对多、多对一、多对多。
一.什么是一对一.一对多.多对一及多对多关系(以简单的学生和老师的关系为例来说): 1.一对一:学生具有学号和姓名(假定没有同名的学生)这两个属性,那么我知道了学生的学号也就能找到对应的学生姓名,如果 ...
- Mybatis表关联一对多、多对一、多对多
项目工程结构如下: 1. 搭建MyBatis框架环境 首先需要引入两个包:mybatis.jar 和 sqljdbc42.jar包 若分页需要导入两个包:pagehelper-5.1.0.jar 和 ...
- django(八)之数据库表的一对多,多对多表-增删改查
单表操作 表记录的添加 方式一: Book() b=Book(name="python基础",price=99,author="yuan",pub_date=& ...
- SSH框架的多表查询和增删查改 (方法一)中
原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html 这边文章是接的刚刚前一遍的基础上敲的 ...
- Django多表查询
一.前言 1.什么是ORM? ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候, ...
- .NetCore中EFCore的使用整理(二)-关联表查询
EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core 1.1 1.当前的版本,还不支持延迟加载(Lazy ...
随机推荐
- java写入文件的几种方法分享
转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...
- URL和URI的区别与联系
转自:http://win7452.blog.51cto.com/147513/45741 今天在看STRUTS配置的时候,发现一个问题,就是在看配置文件的时候,有时出现URL有时又是URI, 让我心 ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
- loj 1013(LCS+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25839 思路:第一小问可以很快求出了,两个字符串的长度-LCS,然 ...
- 64位环境中使用SQL查询excel的方式解决
--64位环境中使用SQL查询excel的方式 环境: OS:Windows Server 2008 R2 Enterprise MSSQL:Microsoft SQL Server 2008 R2 ...
- 用戶登陸。防SQL注入,驗證碼不區分大小寫
if (string.Compare(TBCheckCode.Text, Session["CheckCodeI"].ToString(), true) == 0) ...
- 利用mapreduce将数据从hdfs导入到hbase遇到的问题
现象: 15/08/12 10:19:30 INFO mapreduce.Job: Job job_1439396788627_0005 failed with state FAILED due to ...
- 利用jquery给指定的table动态添加一行、删除一行
转自:http://www.cnblogs.com/linjiqin/p/3148181.html $("#mytable tr").find("td:nth-child ...
- Android横屏竖屏切换的问题
Android横屏竖屏切换的问题 http://blog.sina.com.cn/s/blog_77c632410101790w.html
- ZOJ 3903 Ant ZOJ Monthly, October 2015 - A
Ant Time Limit: 1 Second Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...