这里使用的Teacher类和Student类(假设一个Teacher对应多个学生,一个学生对应一个老师)
所需jar包
开始建表
1(表名 teacher)
2(表名 student)
主键都为自增长
创建实体类
Teacher类
package com.bright.po;
import java.util.Set;
public class Teacher {
private Integer id;
private String name;
private Integer age;
private Set students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", age=" + age
+ ", students=" + students + "]";
}
}
Student类
package com.bright.po;
public class Student {
private Integer id;
private String name;
private Teacher teacher;
public Student() {
super();
}
public Student(String name, Teacher teacher) {
super();
this.name = name;
this.teacher = teacher;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
配置Teacher类的映射文件
配置Student类的映射文件
配置Hibrnate的配置文件
com.mysql.jdbc.Driver
jdbc:mysql:///me
root
root
true
true
org.hibernate.dialect.MySQLDialect
表中插入几条数据
teacher表
student表
开始测试
首先写个获取Session的工具类
package com.bright.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionUtils {
public static Session getSession(){
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
return session;
}
public static void closeSession(Session session){
if(session!=null){
session.getTransaction().commit();
session.close();
}
}
}
具体测试
package com.bright.test;
import java.util.List;
import org.hibernate.Session;
import com.bright.po.Student;
import com.bright.po.Teacher;
import com.bright.utils.SessionUtils;
public class TestTeacher {
public static void main(String[] args) {
Session session = SessionUtils.getSession();
List list = session.createQuery("from Teacher").list();
for(Teacher t:list){
System.out.println(t);
}
SessionUtils.closeSession(session);
}
}
输出结果
Hibernate:
select
teacher0_.id as id1_1_,
teacher0_.name as name2_1_,
teacher0_.age as age3_1_
from
me.teacher teacher0_
Hibernate:
select
students0_.t_id as t_id3_0_0_,
students0_.id as id1_0_0_,
students0_.id as id1_0_1_,
students0_.name as name2_0_1_,
students0_.t_Id as t_Id3_0_1_
from
me.student students0_
where
students0_.t_id=?
Teacher [id=1, name=白起, age=27, students=[Student [id=2, name=苏烈], Student [id=3, name=刘邦], Student [id=1, name=亚瑟]]]
Hibernate:
select
students0_.t_id as t_id3_0_0_,
students0_.id as id1_0_0_,
students0_.id as id1_0_1_,
students0_.name as name2_0_1_,
students0_.t_Id as t_Id3_0_1_
from
me.student students0_
where
students0_.t_id=?
Teacher [id=2, name=宫本武藏, age=25, students=[Student [id=6, name=兰陵王], Student [id=4, name=孙悟空], Student [id=5, name=荆轲]]]
Hibernate:
select
students0_.t_id as t_id3_0_0_,
students0_.id as id1_0_0_,
students0_.id as id1_0_1_,
students0_.name as name2_0_1_,
students0_.t_Id as t_Id3_0_1_
from
me.student students0_
where
students0_.t_id=?
Teacher [id=3, name=后羿, age=55, students=[Student [id=7, name=百里守约], Student [id=9, name=公孙离], Student [id=8, name=马可波罗]]]
可以看出,查询执行了多次,多次生成了sql语句,效率低,所以接下来采用了表的“迫切左外连接”。具体代码:
package com.bright.test;
import java.util.List;
import org.hibernate.Session;
import com.bright.po.Student;
import com.bright.po.Teacher;
import com.bright.utils.SessionUtils;
public class TestTeacher {
public static void main(String[] args) {
Session session = SessionUtils.getSession();
List list = session.createQuery("select t from Teacher t left outer join fetch t.students").list();
for(Teacher t:list){
System.out.println(t);
}
SessionUtils.closeSession(session);
}
}
查询结果:
Hibernate:
select
teacher0_.id as id1_1_0_,
students1_.id as id1_0_1_,
teacher0_.name as name2_1_0_,
teacher0_.age as age3_1_0_,
students1_.name as name2_0_1_,
students1_.t_Id as t_Id3_0_1_,
students1_.t_id as t_id3_0_0__,
students1_.id as id1_0_0__
from
me.teacher teacher0_
left outer join
me.student students1_
on teacher0_.id=students1_.t_id
Teacher [id=1, name=白起, age=27, students=[Student [id=3, name=刘邦], Student [id=1, name=亚瑟], Student [id=2, name=苏烈]]]
Teacher [id=1, name=白起, age=27, students=[Student [id=3, name=刘邦], Student [id=1, name=亚瑟], Student [id=2, name=苏烈]]]
Teacher [id=1, name=白起, age=27, students=[Student [id=3, name=刘邦], Student [id=1, name=亚瑟], Student [id=2, name=苏烈]]]
Teacher [id=2, name=宫本武藏, age=25, students=[Student [id=4, name=孙悟空], Student [id=5, name=荆轲], Student [id=6, name=兰陵王]]]
Teacher [id=2, name=宫本武藏, age=25, students=[Student [id=4, name=孙悟空], Student [id=5, name=荆轲], Student [id=6, name=兰陵王]]]
Teacher [id=2, name=宫本武藏, age=25, students=[Student [id=4, name=孙悟空], Student [id=5, name=荆轲], Student [id=6, name=兰陵王]]]
Teacher [id=3, name=后羿, age=55, students=[Student [id=8, name=马可波罗], Student [id=7, name=百里守约], Student [id=9, name=公孙离]]]
Teacher [id=3, name=后羿, age=55, students=[Student [id=8, name=马可波罗], Student [id=7, name=百里守约], Student [id=9, name=公孙离]]]
Teacher [id=3, name=后羿, age=55, students=[Student [id=8, name=马可波罗], Student [id=7, name=百里守约], Student [id=9, name=公孙离]]]
这样就好多了
结构图