Hibernate多对多创建表的时候有点区别,他们应该有一张共同的ID关联表,关联他们的ID;创建的表总共有三张。
使用学生和课程表举例
实体 都生成相应的get set方法 和无参构造 重写toString的时候Set集合可以不用重写
学生的实体
private Integer stuID; // 学生的ID
private String stuName; // 学生的名字
private Set<Course> course = new HashSet<>(); // 课程表的集合
课程表的实体
private Integer couID; // 课程的ID
private String couName; // 课程的名称
private Set<Student> students = new HashSet<>(); // 学生的集合
学生表的配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 4.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.text.hibernate.model.Student" table="student">
<id name="stuID" column="stu_id" type="java.lang.Integer"> <!-- 主键 -->
<generator class="assigned"></generator> <!-- 属性及意义 -->
</id>
<property name="stuName" column="stu_name" type="java.lang.String" length="50"></property> <!-- name对应实体column对应数据库 -->
<!--<many-to-one name="userAdrs" class="com.khbr.hibernate.userAdrs" cascade="save-update" >-->
<!--<column name="adrs_id" ></column>-->
<!--</many-to-one>-->
<set name="course" cascade="save-update" table="stu_couse" >
<key column="SID"></key> <!-- 关联学生表的ID -->
<!-- 多对多使用 <many-to-many> 和学生表对应的课程表实体 并设置它的column -->
<many-to-many class="com.text.hibernate.model.Course" column="CID"></many-to-many>
</set>
</class>
</hibernate-mapping>
课程表的配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 4.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.text.hibernate.model.Course" table="course">
<id name="couID" column="cou_id" type="java.lang.Integer"> <!-- 主键 -->
<generator class="assigned"></generator> <!-- 属性及意义 -->
</id>
<property name="couName" column="cou_name" type="java.lang.String" length="50"></property> <!-- name对应实体column对应数据库 -->
<set name="students" cascade="save-update" table="stu_couse" inverse="true">
<key column="CID"></key><!-- 关联课程表的ID -->
<!-- 多对多使用 <many-to-many> 和课程对应的学生表实体 并设置它的column -->
<many-to-many class="com.text.hibernate.model.Student" column="SID"></many-to-many>
</set>
</class>
</hibernate-mapping>
注意:inverse="true"两个配置文件中只需要配置一个即可!
Hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--加载数据源-->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property> <!-- 登录账号 -->
<property name="hibernate.connection.password">xxxx</property> <!-- 登录密码 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=GMT%2B8</property>
<!--加载的是什么数据库-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property> <!-- 打印sql语句 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 自动建表 -->
<!--<mapping resource="mappddd/Course.hbm.xml"></mapping> <!– 加载实体类的Mysql对应xml文件 –>-->
<!--<mapping resource="mappddd/Student.hbm.xml"></mapping> <!– 加载实体类的Mysql对应xml文件 –>-->
</session-factory>
</hibernate-configuration>
测试类
import com.khbr.hibernate.model.Course;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class text {
public static void main(String[]args){
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
/* Student stu1 = new Student(); // 第一个学生李四
stu1.setStuID(1);
stu1.setStuName("李四");
Student stu2 = new Student(); // 第二个学生张三
stu2.setStuID(2);
stu2.setStuName("张三");
Student stu3 = new Student(); // 第三个学生王五
stu3.setStuID(3);
stu3.setStuName("王五");
Course cou1 = new Course(); // 第一门课程 语文
cou1.setCouID(10);
cou1.setCouName("语文");
Course cou2 = new Course(); // 第二门课程数学
cou2.setCouID(11);
cou2.setCouName("数学");
Course cou3 = new Course(); // 第三门课程英语
cou3.setCouID(12);
cou3.setCouName("英语");
stu1.getCourse().add(cou1); // 第一个李四有语文和数学两个课程
stu1.getCourse().add(cou2);
stu2.getCourse().add(cou1); // 第二个张三有语文和英语两门课程
stu2.getCourse().add(cou3);
stu3.getCourse().add(cou1); // 第三个王五三门课程都有
stu3.getCourse().add(cou2);
stu3.getCourse().add(cou3);
session.saveOrUpdate(stu1); // 保存第一位同学的课程
session.saveOrUpdate(stu2); // 保存第二位同学的课程
session.saveOrUpdate(stu3); // 保存第三位同学的课程
transaction.commit(); // 提交事物
session.close(); //关闭session*/
// 从学生查询课程
/*Student stu = (Student)session.get(Student.class,1);
System.out.println(stu);
System.out.println(stu.getCourse());*/
// 从课程查询学生
Course cou = (Course)session.get(Course.class,10);
System.out.println(cou);
System.out.println(cou.getStudents());
}
}