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的多表查询——内连接、外连接的更多相关文章

  1. 【SQL】多表查询中的 外连接 ,on,where

    先简单粗暴给个结论,多表连结查询中,on比where更早起作用,系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由where进行匹配过滤,where后语句为真,则能查询出来,而通过外连接 ...

  2. mysql数据库中的多表查询(内连接,外连接,子查询)

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  3. 知识点:Oracle+表连接方式(内连接-外连接-自连接)+详解 来自百度文库

    Oracle 表之间的连接分为三种: 1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制)        (2)右外连接(右边的表不加限制)        (3)全外连接(左右 ...

  4. Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符)

    Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符) 一丶多表查询     多表连接查询的应用场景: ​         连接是关系数据库模型的主要特点,也是区别于其他 ...

  5. sql内连接外连接自然连接

    为什么我们要使用内连接和外连接呢?可以从两张或者多张表中找出,我们需要的属性. 这个比较好:http://www.cnblogs.com/youzhangjin/archive/2009/05/22/ ...

  6. Day055--MySQL--外键的变种,表与表的关系,单表查询,多表查询, 内连接,左右连接,全外连接

    表和表的关系 ---- 外键的变种 * 一对多或多对一 多对多 一对一 参考 https://www.cnblogs.com/majj/p/9169416.html 如何找出两张表之间的关系 分析步骤 ...

  7. 【cl】多表查询(内、外连接)

    交叉连接(cross join):该连接产生的结果集笛卡尔积 a有7行,b有8行    a的第一行与b的每一行进行连接,就有8条a得第一行 7*8=56条 select a.real_name,s.u ...

  8. 08_MySQL DQL_SQL99标准中的多表查询(内连接)

    # sql99语法/*语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [where 筛选条件] [group by 分组] [having 分 ...

  9. oracle 内连接 外连接 查询 笔记

    elect ename,job,sal from emp where deptno>10 order by sal desc; 联合查询,PK dept.deptno FK emp.deptno ...

随机推荐

  1. Vue项目(vuecli3.0搭建)集成高德地图实现路线轨迹绘制

    先看最后实现的效果图 高德地图api文档 https://lbs.amap.com/api/javascript-api/summary 使用 1.在index.html里面引入高德地图js文件 2. ...

  2. c#中关于@的作用

    参考链接:https://www.cnblogs.com/linkbiz/p/6380814.html

  3. 手机端 关闭当前页面的JS

    JS 代码如下   PS:我找的原代码中, 注释部分是没有注释的,但是调试的时候报错,就注释掉了,因为我只需要微信和支付宝的判定,所以这个地方,就没仔细研究 var isLppzApp = false ...

  4. java 枚举示例

    public enum YNEnum { N(0,"否"), Y(1,"是"); private int code; private String name; ...

  5. Workerman启动与停止相关命令

    start.php为入口文件 一.启动 1.以debug(调试)方式启动 php start.php start 2.以daemon(守护进程)方式启动 php start.php start -d ...

  6. CSS 案例

    一.滑动门案例 二.小黄人案例 三.圣杯布局&双飞翼布局

  7. Apache加固之目录、文件限制

    如果你用类似phpstudy集成平台的话,所有你想要修改的配置基本上在phpstudy上就可以直接设置操作.但如果你的服务器是通过一步步安装的(Apache+PHP+Mysql)的话,那么就要对各功能 ...

  8. Android-----解析xml文件的三种方式

    SAX解析方法介绍: SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备.SAX解析XML文件采用的是事件驱动,也就是说, ...

  9. IVS_原理

    智能视频分析技术指计算机图像视觉分析技术,是人工智能研究的一个分支,它在图像及图像描述之间建立映射关系,从而使计算机能够通过数字图像处理和分析来理解视频画面中的内容.智能视频分析技术涉及到模式识别.机 ...

  10. day 28

    目录 操作系统发展史 穿孔卡片 联机批处理系统 脱机批处理系统 多道技术(基于单核情况下研究) 单道 多道技术 并发与并行 进程 程序与进程 进程调度 先来先服务调度 短作业优先调度 时间片轮转法 分 ...