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();//游离状态 }