HOL的多表查询——内连接、外连接
1、内连接:
由于学生和班级是多对一的关系,班级对应学生是一对多的关系,因此,需要先对学生和班级进行配置。
(1)创建Student类(多的一方):
package pers.zhb.domain;
public class Student {
private int studentno;
private String sname;
private String sex;
private String birthday;
private String classno;
private Float point;
private String phone;
private Clas aClas;
public Student(){//无参的构造方法
}
public Clas getaClas() {
return aClas;
}
public void setaClas(Clas aClas) {
this.aClas = aClas;
}
public int getStudentno() {
return studentno;
} public void setStudentno(int studentno) {
this.studentno = studentno;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getBirthday() {
return birthday;
} public void setBirthday(String birthday) {
this.birthday = birthday;
} public String getClassno() {
return classno;
} public void setClassno(String classno) {
this.classno = classno;
} public float getPoint() {
return point;
} public void setPoint(float point) {
this.point = point;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} @Override
public String toString() {
return "Student{" +
"studentno='" + studentno + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", birthday='" + birthday + '\'' +
", classno='" + classno + '\'' +
", point=" + point +
", phone='" + phone + '\'' +
'}';
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pers.zhb.domain">
<class name="Student" table="student">
<id name="studentno" column="studentno">
<generator class="native"></generator>
</id>
<property name="birthday" column="birthday"></property>
<property name="classno" column="classno" insert="false" update="false"></property>
<property name="phone" column="phone"></property>
<property name="sex" column="sex"></property>
<property name="sname" column="sname"></property>
<property name="point" column="point"></property>
<many-to-one name="aClas" column="classno" class="Clas"></many-to-one>
</class>
</hibernate-mapping>
(2)创建Clas类(班级,代表1的一方):
package pers.zhb.domain;
import java.util.HashSet;
import java.util.Set;
public class Clas {
private String classno;
private String department;
private String monitor;
private String classname;
private Set<Student> students=new HashSet<Student>();//使用set集合表达一对多关系,一个班级对应多个学生
public String getClassno() {
return classno;
} public void setClassno(String classno) {
this.classno = classno;
} public String getDepartment() {
return department;
} public void setDepartment(String department) {
this.department = department;
} public String getMonitor() {
return monitor;
} public void setMonitor(String monitor) {
this.monitor = monitor;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getClassname() {
return classname;
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} @Override
public String toString() {
return "Clas{" +
"classno=" + classno +
", department='" + department + '\'' +
", monitor='" + monitor + '\'' +
", classname='" + classname + '\'' +
", students=" + students +
'}';
} }
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pers.zhb.domain">
<class name="Clas" table="class">
<id name="classno" column="classno">
<generator class="native"></generator>
</id><!--主键-->
<property name="department" column="department"></property>
<property name="monitor" column="monitor"></property>
<property name="classname" column="classname"></property>
<set name="students" table="student"><!--一对多关系配置-->
<key column="classno" update="false"></key><!--指定了集合表的外键-->
<one-to-many class="Student"></one-to-many>
</set>
</class>
</hibernate-mapping>
(3)测试HQL的内连接:
public static void testSel() {
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Clas c inner join c.students";
Query query=session.createQuery(hql);
List<Object[]> list=query.list();
for(Object[] arr:list){
System.out.println(Arrays.toString(arr));
}
transaction.commit();
session.
表中数据:
学生表:

班级表:

测试结果:

通过SQL语句直接查询:
SELECT *
FROM student,class
WHERE student.classno=class.classno
AND student.classno='tx171'

(4)迫切内连接:
public static void testSel() {
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Clas c inner join fetch c.students";
Query query=session.createQuery(hql);
List<Clas> list=query.list();
transaction.commit();
session.close();//游离状态
}

与内连接不同的是,迫切内连接是把学生对象直接封装到了班级对象中了,而内连接则是将两个对象存储到了数组中。
2、外连接:
(1)左外连接:
public static void testSel() {
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Clas c left join c.students";
Query query=session.createQuery(hql);
List<Clas> list=query.list();
System.out.println(list);
transaction.commit();
session.close();//游离状态
}
(2)右外连接:
public static void testSel() {
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="from Clas c right join c.students";
Query query=session.createQuery(hql);
List<Clas> list=query.list();
System.out.println(list);
transaction.commit();
session.close();//游离状态
}
HOL的多表查询——内连接、外连接的更多相关文章
- 【SQL】多表查询中的 外连接 ,on,where
先简单粗暴给个结论,多表连结查询中,on比where更早起作用,系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由where进行匹配过滤,where后语句为真,则能查询出来,而通过外连接 ...
- mysql数据库中的多表查询(内连接,外连接,子查询)
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...
- 知识点:Oracle+表连接方式(内连接-外连接-自连接)+详解 来自百度文库
Oracle 表之间的连接分为三种: 1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右 ...
- Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符)
Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符) 一丶多表查询 多表连接查询的应用场景: 连接是关系数据库模型的主要特点,也是区别于其他 ...
- sql内连接外连接自然连接
为什么我们要使用内连接和外连接呢?可以从两张或者多张表中找出,我们需要的属性. 这个比较好:http://www.cnblogs.com/youzhangjin/archive/2009/05/22/ ...
- Day055--MySQL--外键的变种,表与表的关系,单表查询,多表查询, 内连接,左右连接,全外连接
表和表的关系 ---- 外键的变种 * 一对多或多对一 多对多 一对一 参考 https://www.cnblogs.com/majj/p/9169416.html 如何找出两张表之间的关系 分析步骤 ...
- 【cl】多表查询(内、外连接)
交叉连接(cross join):该连接产生的结果集笛卡尔积 a有7行,b有8行 a的第一行与b的每一行进行连接,就有8条a得第一行 7*8=56条 select a.real_name,s.u ...
- 08_MySQL DQL_SQL99标准中的多表查询(内连接)
# sql99语法/*语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [where 筛选条件] [group by 分组] [having 分 ...
- oracle 内连接 外连接 查询 笔记
elect ename,job,sal from emp where deptno>10 order by sal desc; 联合查询,PK dept.deptno FK emp.deptno ...
随机推荐
- UML统一建模语言介绍
统一建模语言简介 统一建模语言(Unified Modeling Language,UML)是用来设计软件蓝图的可视化建模语言,1997 年被国际对象管理组织(OMG)采纳为面向对象的建模语言的国际标 ...
- 【数据库】Mysql配置参数
vim /ect/my.cnf 使用命令打开mysql的配置文件. 加入以下参数 [mysql] default-character-set=utf8 [mysqld] lower_case_tabl ...
- vim中常用折叠命令
最常用3个折叠命令 .反复打开关闭折叠:za (意思就是,当光标处折叠处于打开状态,za关闭之,当光标处折叠关闭状态,打开之) .打开全部折叠:zR .关闭全部折叠:zM 小试折叠: :set fdm ...
- SAP替代,出口U904在RGGBS000中未生成
报错.提示出口U904在RGGBS000中未生成. 一般情况下需要到 程序RGGBS000 中,在form:get_exit_titles 中增加下列代码. exits-name = 'U904. e ...
- DRF的APIView、GenericAPIView、GenericViewSet的原理分析
一.层次结构 GenericViewSet(ViewSetMixin, generics.GenericAPIView) ---DRF GenericAPIView(views.APIView) -- ...
- ELK部署配置使用记录
为什么要用ELK: 一般我们需要进行日志分析场景:直接在日志文件中 grep.awk 就可以获得自己想要的信息.但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档.文本搜索太慢怎么办 ...
- python 读取.mat文件
导入所需包 from scipy.io import loadmat 读取.mat文件 随便从下面文件里读取一个: m = loadmat('H_BETA.mat') # 读出来的 m 是一个dict ...
- SpringCloud-Consul开发环境配置
一.consul安装 1.下载:https://www.consul.io/downloads.html: 2.选择版本:本人开发环境是windows,所以选择win64: 3.安装:保存至D:/Sp ...
- Java自学-接口与继承 UML图
UML 图 步骤 1 : UML 图 -- 类之间的关系 UML-Unified Module Language 统一建模语言,可以很方便的用于描述类的属性,方法,以及类和类之间的关系 步骤 2 : ...
- 第一次Git上传本地项目到github上 的命令
1.下载Git软件:https://git-scm.com/downloads, 2.下载之后安装就很简单了, 3.邮箱注册 在git bash界面输入如下内容即可完成邮箱的注册: $ git con ...