目的
学会配置Hibernate的关系映射
实验步骤
1、创建HibernateUtil类用于管理sessionFactory
HibernateUtil.java
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration()
.configure();
sessionFactory = configuration.buildSessionFactory();
}
catch (Throwable ex) {
System.err.println("初始化sessionFactory失败." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal<Session> session
= new ThreadLocal<Session>();
public static Session currentSession() throws HibernateException {
Session s = session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = session.get();
if (s != null) {
s.close();
}
session.set(null);
}
}
2、创建Student类和Course类
Student.java
public class Student {
private Integer id;
private String studentNo;
private String name;
//private Course course;
private Set<Course> courses = new HashSet<>();
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// public Course getCourse() {
// return course;
// }
//
// public void setCourse(Course course) {
// this.course = course;
// }
}
Course.java
public class Course {
private Integer courseNo;
private String courseName;
private Set<Student> students = new HashSet<>();
// private Student student;
//
// public Student getStudent() {
// return student;
// }
//
// public void setStudent(Student student) {
// this.student = student;
// }
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public Integer getCourseNo() {
return courseNo;
}
public void setCourseNo(Integer courseNo) {
this.courseNo = courseNo;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
3、对两个类进行配置
Student.hbm.xml
Course.hbm.xml
多对一
一对多
多对多
4、配置hibernate.cfg.xml
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=utf8</property>
<property name="connection.username">root</property>
<property name="connection.password">ls123456</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<!--
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_statements">0</property>
-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/bnuz/domain/Student.hbm.xml"/>
<mapping resource="com/bnuz/domain/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5、创建mapping进行测试
mapping.java
public class Mapping {
public static void main(String[] args) {
Mapping mapping = new Mapping();
//mapping.OneWayManyToOne();
//mapping.TwoWayOneToMany();
mapping.TwoWayManyToMany();
}
// public void OneWayManyToOne(){
// Session session = HibernateUtil.currentSession();
// Transaction tx = session.beginTransaction();
// Student student = new Student();
// Course course = new Course();
// course.setCourseName("软件测试");
//
// student.setName("Ross");
// student.setStudentNo("17********");
// student.setCourse(course);
//
// session.persist(student);
// tx.commit();
// HibernateUtil.closeSession();
// }
// public void TwoWayOneToMany(){
// Session session = HibernateUtil.currentSession();
// Transaction tx = session.beginTransaction();
//
// Student student = new Student();
// student.setName("apprentice");
// student.setStudentNo("18********");
// session.save(student);
//
// Course oracle = new Course();
// oracle.setCourseName("大型数据库");
// oracle.setStudent(student);
// session.persist(oracle);
// Course j2ee = new Course();
// j2ee.setCourseName("Java企业级应用");
// j2ee.setStudent(student);
// session.persist(j2ee);
//
// tx.commit();
// HibernateUtil.closeSession();
// }
public void TwoWayManyToMany(){
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("Druid");
student.setStudentNo("19********");
session.save(student);
Course cProgram = new Course();
cProgram.setCourseName("C语言");
cProgram.getStudents().add(student);
session.persist(cProgram);
Course database = new Course();
database.setCourseName("数据库");
database.getStudents().add(student);
session.persist(database);
Student student1 = new Student();
student1.setName("Ross");
student1.setStudentNo("17********");
student1.getCourses().add(database);
session.save(student1);
tx.commit();
HibernateUtil.closeSession();
}
}
结果展示+小贴士
- 谁是1对N中的1就使用set
- 多对多关系可以利用中间表转为多对一+一对多处理