HIbernate | 简单对象嵌套实践
src/entity/Course.java
public class Course { private Integer courseNo; private String courseName; 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; } }
src/entity/Student.java
public class Student { private Integer id; private String studentNo; private String name; private Course course; 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; } }
src/entity/Student.hbm.xml
<?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="entity"> <class name="Student" table="student_table"> <id name="id" type="java.lang.Integer"> <generator class="identity"/> </id> <property name="studentNo" /> <property name="name" /> <many-to-one name="course" cascade="all" class="Course" column="course_no" /> </class> </hibernate-mapping>
src/entity/Course.hbm.xml
<?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="entity"> <class name="Course" table="course_table"> <id name="courseNo" column="course_no"> <generator class="identity"/> </id> <property name="courseName" /> </class> </hibernate-mapping>
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</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">1</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="entity/Student.hbm.xml"/> <mapping resource="entity/Course.hbm.xml"/> </session-factory> </hibernate-configuration>
测试类
Session session = hibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Student student = new Student(); Course course = new Course(); course.setCourseName("蛋疼的软件测试"); student.setName("wade"); student.setStudentNo("87937128379821"); student.setCourse(course); session.persist(student); tx.commit(); hibernateUtil.closeSession();