hibernate 关联关系(多对一、多对多、一对一)的配置
1:多对一
一般关系由多的一方来维护
多的一方需要有一方的字段,一的一方要有多的一方的set集合
一方的配置文件: ClientEntity为一的一方,OrderEntity为多的一方
<?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> <class name="com.yuwenhui.entity.ClientEntity" table="t_client" schema="hibernate"> <id name="id" column="id"/> <property name="name" column="name"/> <property name="pawword" column="pawword"/> <property name="blance" column="blance"/> <!-- name 一行保存多方集合的字段 table 数据库中多方的表名称 inverse 是否反转控制权,一般多对一的关联关系由多方来维护 --> <set name="orders" table="t_order" inverse="true"> <key> <!-- name 对应数据中多方表中关联一方的外键 --> <column name="client_id"/> </key> <!-- class 多方的实体类路径 --> <one-to-many class="com.yuwenhui.entity.OrderEntity"/> </set> </class> </hibernate-mapping>
多方的配置文件:
<?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> <class name="com.yuwenhui.entity.OrderEntity" table="t_order" schema="hibernate"> <id name="id" column="id"/> <property name="name" column="name"/> <property name="price" column="price"/> <!-- name="client" 对应多方的实体类中关联一方的字段 class 对应一方的类的路径 column 在数据生成的列的名称 --> <many-to-one name="client" class="com.yuwenhui.entity.ClientEntity" column="client_id" /> </class> </hibernate-mapping>
多对多关系
以学生和课程为列:学生可以有多个课程,课程也可以有多个学生
学生中需要有一个集合用于保存该学生选修的课程,课程中也需要有一个集合用于保存选修了该课程的学生集合,多对多关系中只需要有一行维护关联关系即可,即在一方添加
inverse="true"属性
学生配置文件
<?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> <class name="domain.Student" table="t_student2" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> <generator class="native" /> </id> <property name="name"> <column name="name" sql-type="varchar(20)" length="20" not-null="true"/> </property> <!-- name 学生表中课程的set集合字段名 table 需要自动创建的中间表名 inverse="false" 表示由该类维护关联关系 --> <set name="courses" table="student_course" inverse="false" > <key> <!-- nam 表示中间表中的字段 --> <column name="student_id"/> </key> <!-- class 另一方的类 column 另一方在中间表中的字段 --> <many-to-many class="domain.Course" column="course_id"></many-to-many> </set> </class> </hibernate-mapping>
课程表配置文件
<?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> <class name="domain.Course" table="t_course" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> <generator class="native" /> </id> <property name="name"> <column name="name" sql-type="varchar(20)" length="20" not-null="true"/> </property> <!-- name 学生表中课程的set集合字段名 table 需要自动创建的中间表名 inverse="false" 表示该类不维护关联关系 --> <set name="students" table="student_course" inverse="true" > <key> <column name="course_id"/> </key> <!-- class 另一方的类 column 另一方在中间表中的字段 --> <many-to-many class="domain.Student" column="student_id"></many-to-many> </set> </class> </hibernate-mapping>
多对多测试:
package test; import domain.Course; import domain.Student; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Created by Administrator on 2017/9/8 0008. */ public class Work98Test { SessionFactory sessionFactory; Session session; @Before public void before(){ Configuration configuration = new Configuration(); configuration.configure(); sessionFactory = configuration.buildSessionFactory(); session = sessionFactory.openSession(); } @Test public void testSave(){ Transaction transaction = session.beginTransaction(); Student student = new Student(); Course course = new Course(); course.setName("软件工程"); student.setName("余文辉");
// 学生选修课程 student.getCourses().add(course); session.save(course); session.save(student); transaction.commit(); } @After public void after(){ session.close(); sessionFactory.close(); } }
3:一对一
以学生和学生信息表为列,一个学生对应一张学生信息表,一个学生信息表也只能对应一个学生
需要在对应的表和设置对方的字段,并添加相应的get和set方法
一对一对应分为两种,一种是两张表之间主键对应,另一种是在一张表中设置外键关联,这里演示的是前一种
学生类配置文件
<?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> <class name="domain.Student" table="t_student2" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> <generator class="native" /> </id> <property name="name"> <column name="name" sql-type="varchar(20)" length="20" not-null="true"/> </property> <!-- name 学生表中课程的set集合字段名 table 需要自动创建的中间表名 inverse="false" 表示由该类维护关联关系 --> <set name="courses" table="student_course" inverse="false" > <key> <!-- nam 表示中间表中的字段 --> <column name="student_id"/> </key> <!-- class 另一方的类 column 另一方在中间表中的字段 --> <many-to-many class="domain.Course" column="course_id"></many-to-many> </set> <!-- 设置一对一关联关系 --> <many-to-one name="informationOfStudent" class="domain.InformationOfStudent" column="information_id" unique="true"></many-to-one> </class> </hibernate-mapping>
学生信息类配置文件
<?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> <class name="domain.InformationOfStudent" table="t_information_student" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> <generator class="native"/> </id> <property name="sex"> <column name="sex" sql-type="varchar(20)" length="20" not-null="true"/> </property> <property name="age"> <column name="age" sql-type="int(11)" not-null="true"/> </property> <property name="address"> <column name="address" sql-type="varchar(20)" length="20"/> </property> <property name="tel"> <column name="tel" sql-type="int(11)" not-null="true"/> </property> <!-- 设置一对一关联关系 --> <one-to-one name="student" class="domain.Student"></one-to-one> </class> </hibernate-mapping>
测试类源码
package test; import domain.InformationOfStudent; import domain.Student; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Created by Administrator on 2017/9/8 0008. */ public class One2OneTest { SessionFactory sessionFactory; Session session; @Before public void before(){ Configuration configuration = new Configuration(); configuration.configure(); sessionFactory = configuration.buildSessionFactory(); session = sessionFactory.openSession(); } @Test public void testAddInformation(){ Transaction transaction = session.beginTransaction(); InformationOfStudent information = new InformationOfStudent(); information.setAge(20); information.setSex("男"); information.setAddress("江西省"); information.setTel(10086); session.save(information); transaction.commit(); } @Test public void testAddRelation(){ Transaction transaction = session.beginTransaction(); InformationOfStudent informationOfStudent = (InformationOfStudent) session.get(InformationOfStudent.class,2); Student student = (Student) session.get(Student.class,1); student.setInformationOfStudent(informationOfStudent); session.save(informationOfStudent); session.save(student); transaction.commit(); } @After public void after(){ session.close(); sessionFactory.close(); } }