1 package com.xiaostudy.domain; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 /** 7 * Teacher 8 * 9 * @author xiaostudy 10 * 11 */ 12 public class Teacher { 13 // id 14 private Integer id; 15 // 普通属性 16 private String name; 17 // 另一个类的集合 18 private Set<Student> students = new HashSet<Student>(); 19 20 // 自动生成get、set方法 21 public Integer getId() { 22 return id; 23 } 24 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public Set<Student> getStudents() { 38 return students; 39 } 40 41 public void setStudents(Set<Student> students) { 42 this.students = students; 43 } 44 45 }
1 package com.xiaostudy.domain; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 /** 7 * Student 8 * 9 * @author xiaostudy 10 * 11 */ 12 public class Student { 13 // id 14 private Integer id; 15 // 普通属性 16 private String name; 17 // 另一个类的集合 18 private Set<Teacher> teachers = new HashSet<Teacher>(); 19 20 // 自动生成get、set方法 21 public Integer getId() { 22 return id; 23 } 24 25 public void setId(int id) { 26 this.id = id; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public Set<Teacher> getTeachers() { 38 return teachers; 39 } 40 41 public void setTeachers(Set<Teacher> teachers) { 42 this.teachers = teachers; 43 } 44 45 public void setId(Integer id) { 46 this.id = id; 47 } 48 49 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <!-- 导入包 --> 6 <hibernate-mapping package="com.xiaostudy.domain"> 7 <!-- name为bean的类名,table为数据库的表名 --> 8 <class name="Teacher" table="t_teacher"> 9 <!-- name为bean的id,column为数据库表的列名 --> 10 <id name="id" column="id"> 11 <!-- 设置自动更新表,表没有的话创建一个,有的话,再判断结构是否为所需,如有没有想应得列,会自动添加列,多余的也不会删除 --> 12 <generator class="native"></generator> 13 </id> 14 <!-- name为普通属性,column为数据库表的列名,type为数据类型 --> 15 <property name="name" column="name" type="string"></property> 16 <!-- name为bean中另一个类的集合名称,table为多对多中间的表,cascade为级联设置自动更新表 --> 17 <set name="students" table="t_teacher_student" cascade="save-update"> 18 <!-- 自身连接外面的外键 --> 19 <key column="tid"></key> 20 <!-- column为另一个连接的外键,class为另一个bean的类名 --> 21 <many-to-many column="sid" class="Student"></many-to-many> 22 </set> 23 </class> 24 </hibernate-mapping>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="com.xiaostudy.domain"> 6 <class name="Student" table="t_student"> 7 <id name="id" column="id"> 8 <generator class="native"></generator> 9 </id> 10 <property name="name" column="name" type="string"></property> 11 <set name="teachers" table="t_teacher_student"> 12 <key column="sid"></key> 13 <many-to-many column="tid" class="Teacher"></many-to-many> 14 </set> 15 </class> 16 </hibernate-mapping>
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 注册驱动 --> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <!-- mysql的用户名 --> 11 <property name="connection.username">root</property> 12 <!-- mysql的用户密码 --> 13 <property name="connection.password">123456</property> 14 <!-- 连接mysql的某库 --> 15 <property name="connection.url">jdbc:mysql://localhost:3306/user</property> 16 <!-- 控制台输出sql --> 17 <property name="show_sql">true</property> 18 <!-- 格式化输出的sql --> 19 <property name="format_sql">true</property> 20 <!-- 自动提交事务 --> 21 <!-- <property name="connection.autocommit">true</property> --> 22 <!-- 创建sql表 23 update:如果没有表,则创建一个。如果有表,而且表结构一致,那么不改变表。如果表结构不一样,会添加sql表缺少的列,多余的也不会删除。 24 create:不管sql表有没有存在,都会重新创建表。 25 create-drop:在create的基础上,每次关闭虚拟机时都会把表删除了。 26 validate:效验sql表,如果一致,则没有反应,如果不一致了,会抛出异常。 27 --> 28 <property name="hbm2ddl.auto">update</property> 29 <!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession --> 30 <property name="current_session_context_class">thread</property> 31 <!-- 数据库方言配置 --> 32 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 33 <!-- 导入映射文件 --> 34 <mapping resource="com/xiaostudy/domain/Teacher.hbm.xml"/> 35 <mapping resource="com/xiaostudy/domain/Student.hbm.xml"/> 36 </session-factory> 37 </hibernate-configuration>
1 package com.xiaostudy.test; 2 3 import org.hibernate.classic.Session; 4 5 import com.xiaostudy.domain.Student; 6 import com.xiaostudy.domain.Teacher; 7 import com.xiaostudy.util.HibernateUtils; 8 9 /** 10 * 测试多对多 11 * 12 * @author xiaostudy 13 * 14 */ 15 public class Test1 { 16 17 public static void main(String[] args) { 18 //根据hibernate工具类,获取一个全新的Session 19 Session session = HibernateUtils.openSession(); 20 //开始事务 21 session.beginTransaction(); 22 Teacher teacher1 = new Teacher(); 23 Teacher teacher2 = new Teacher(); 24 teacher1.setName("t1"); 25 teacher2.setName("t2"); 26 27 Student student1 = new Student(); 28 Student student2 = new Student(); 29 student1.setName("s1"); 30 student2.setName("s2"); 31 32 //维护关系 33 teacher1.getStudents().add(student1); 34 teacher1.getStudents().add(student2); 35 teacher2.getStudents().add(student1); 36 teacher2.getStudents().add(student2); 37 38 //持久化数据 39 session.save(teacher1); 40 session.save(teacher2); 41 42 //关闭事务并提交事务 43 session.getTransaction().commit(); 44 //关闭资源 45 session.close(); 46 } 47 48 }
码云:https://gitee.com/xiaostudy2/hibernate_manyToMany_demo/attach_files
原文:https://www.cnblogs.com/xiaostudy/p/9520897.html