dljd_(071-074)_hibernate_多对多关联关系映射
一、多对多关联关系映射
多对多关联关系映射其实就是两个一对多关联关系映射组成的。要用到中间表(中间表充当多方)
二、示例(一个学生可以选多门课程、一门课程可以被多个学生选)
1.1定义学生实体
package edu.aeon.beans; import java.util.HashSet; import java.util.Set; /** * [说明]:学生实体类 * @author aeon * */ public class Student { private Integer stuId; private String stuName; private Set<Course> courses; public Student() { courses=new HashSet<Course>(); } public Student(String stuName){ this(); this.stuName=stuName; } public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } @Override public String toString() { return "Student [stuId=" + stuId + ", stuName=" + stuName + ", courses=" + courses + "]"; } }
1.2Student.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="edu.aeon.beans"> <class name="Student" table="student"> <id name="stuId"> <column name="stuid" sql-type="int(3)"/> <generator class="native"/> </id> <property name="stuName"> <column name="stuname" sql-type="varchar(16)"/> </property> <set name="courses" cascade="save-update" table="student_course"> <key column="stuid"/> <many-to-many class="Course" column="cid" /> </set> </class> </hibernate-mapping>
1.3课程实体类(Course)
package edu.aeon.beans; import java.util.HashSet; import java.util.Set; /** * [说明]:课程实体类 * @author aeon * */ public class Course { private Integer cid; private String cname; private Set<Student> students; public Course() { this.students=new HashSet<Student>(); } public Course(String cname) { this(); this.cname = cname; } public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } @Override public String toString() { return "Course [cid=" + cid + ", cname=" + cname + ", students=" + students + "]"; } }
1.4Course.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="edu.aeon.beans"> <class name="Course" > <id name="cid"> <column name="cid" sql-type="int(3)"/> <generator class="native"/> </id> <property name="cname"> <column name="cname" sql-type="varchar(16)"/> </property> <set name="students" cascade="save-update" table="student_course"> <key column="cid"/> <many-to-many class="Student" column="stuid" /> </set> </class> </hibernate-mapping>
1.5将两个实体注册到主配置文件中
<?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> <!-- 配置数据库的四要素 --> <property name="hibernate.connection.driver">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 配置数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 配置数据库连接池 --> <!-- <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> --> <!-- 注册当前session上下文 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 自动建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 显示sql --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql --> <property name="hibernate.format_sql">true</property> <mapping resource="edu/aeon/beans/Student.hbm.xml"/> <mapping resource="edu/aeon/beans/Course.hbm.xml"/> </session-factory> </hibernate-configuration>
1.6测试类
package edu.aeon.hibernate.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import edu.aeon.aeonutils.hibernate.getsessionutil.GetSessionUtil; import edu.aeon.beans.Course; import edu.aeon.beans.Student; /** * [说明]:测试多对多关联关系映射 * @author aeon * */ public class TestMany2Many { @Test public void testSave() { Session session=null; Transaction transaction=null; try { session = GetSessionUtil.getSession(); transaction=session.getTransaction(); transaction.begin(); Course course1=new Course("c"); Course course2=new Course("andriod"); Course course3=new Course("java"); Student student1=new Student("zhangsan"); Student student2=new Student("lisi"); student1.getCourses().add(course1); student1.getCourses().add(course3); student2.getCourses().add(course3); student2.getCourses().add(course2); session.save(student1); session.save(student2); transaction.commit(); } catch (Exception e) { e.printStackTrace(); transaction.rollback(); }finally { if(null!=session){ session.close(); } } } }
测试结果:
Hibernate: create table Course ( cid int(3) not null auto_increment, cname varchar(16), primary key (cid) ) Hibernate: create table student ( stuid int(3) not null auto_increment, stuname varchar(16), primary key (stuid) ) Hibernate: create table student_course ( stuid integer not null, cid integer not null, primary key (cid, stuid) ) Hibernate: alter table student_course add constraint FKh5wih1h6w7ou383ollpltc3o foreign key (cid) references Course (cid) Hibernate: alter table student_course add constraint FK1psvvjefhfaxlelcr844pwttv foreign key (stuid) references student (stuid) Hibernate: insert into student (stuname) values (?) Hibernate: insert into Course (cname) values (?) Hibernate: insert into Course (cname) values (?) Hibernate: insert into student (stuname) values (?) Hibernate: insert into Course (cname) values (?) Hibernate: insert into student_course (stuid, cid) values (?, ?) Hibernate: insert into student_course (stuid, cid) values (?, ?) Hibernate: insert into student_course (stuid, cid) values (?, ?) Hibernate: insert into student_course (stuid, cid) values (?, ?)
测试结果截图:
数据库数据信息:
inverse赋值为true,表示一方放弃关联关系(对应到数据库中是不维护主外键关系)的维护权。这是一方的特权,多方不具有。