知识点:
【
多对多(teacher - student)
在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型;Hibernate会为我们创建中间关联表,转换成两个一对多。
<set name="teacher" table="teacher_student">
<key column="teacher_id"/>
<many-to-many class="Student" column="student_id"/>
</set>
】
照样实现步骤如下:
第一步:搭建hibernate运行所需的环境及*.jar 具体搭建见上面的博客
hibernate.cfg.xml:
<!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="show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- jdbc:mysql:///test 以下表示localhost 3306 test:数据库-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">liyong</property>
<!-- 下面指定方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 下面指定是否让hibernate产生ddl语句 DDL:数据定义语言-->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/liyong/domain/Student.hbm.xml"/>
<mapping resource="com/liyong/domain/Teacher.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第二步:编程实体类Student.java 和Teacher.java
Student.java
public class Student {
private int id;
private String name;
private Set<Teacher> teachers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
Teacher.java
public class Teacher {
private int id;
private String name;
private Set<Student> students;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
第三步:编写类的映射文件Student.hbm.xml、Teacher.hbm.xml
Teacher.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.liyong.domain">
<class name="Teacher">
<!-- 生成主键 以natvie 自增长-->
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<!-- 下面的table指定中间表名table -->
<set name="students" table="teacher_student">
<key column="teachar_id"/>
<!--下面是所映射的外键-->
<many-to-many class="Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.liyong.domain">
<class name="Student">
<!-- 生成主键 以natvie 自增长-->
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<!-- 下面的table指定中间表名table -->
<set name="teachers" table="teacher_student">
<!--<key column="student_id"/>表示查询teachers这个集合中的数据时根据通过反射找到Teacher类所映射的表中的 外键student_id列对应去查询 -->
<key column="student_id"/>
<!-- teachar_id 是映射中间表的列名 可以看成就是映射的外键-->
<many-to-many class="Teacher" column="teachar_id"/>
</set>
</class>
</hibernate-mapping>
第四步:编程测试
public class ManyToManyTest {
public static void main(String[] args) {
//add();
query(1);
}
@Test
public static void add() {
Session s = null;
Transaction tx = null;
try {
Set<Teacher> ts = new HashSet<Teacher>();
Teacher t1 = new Teacher();
t1.setName("t1 name");
ts.add(t1);
Teacher t2 = new Teacher();
t2.setName("t2 name");
ts.add(t2);
Set<Student> ss = new HashSet<Student>();
Student s1 = new Student();
s1.setName("s1");
ss.add(s1);
Student s2 = new Student();
s2.setName("s2");
ss.add(s2);
t1.setStudents(ss);
t2.setStudents(ss);
/* 不能同时学生和老师添加集合数据
s1.setTeachers(ts);
s2.setTeachers(ts);*/
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(t1);
s.save(t2);
s.save(s1);
s.save(s2);
tx.commit();
} finally {
if (s != null)
s.close();
}
}
@Test
public static void query(int id)
{
Session s = null;
try {
s=HibernateUtil.getSession();
Teacher teacher=(Teacher)s.get(Teacher.class, id);
Set<Student> students=teacher.getStudents();
System.out.println("teacher :"+teacher.getName());
for(Student student:students)
{
System.out.println("name_:"+student.getName());
}
}catch (Exception e) {
throw new RuntimeException("error");
}finally{
if(s!=null)
{
s.close();
}
}
}
}
第五:测试
源码见附件:
hibernate多对多关联映射
原创青年IT男1 博主文章分类:Hibernate学习笔记 ©著作权
文章标签 hibernate多对多关联映射 hibernate xml mysql 文章分类 前端开发
上一篇:hibernate多对一关联映射
下一篇:懒加载
-
Hibernate映射——多对多关联映射(八)
上篇博文【SSH进阶之路】Hibernate映射——一对多关联映射(据冗余的问题。
hibernate mybatis orm 映射 table -
Hibernate - 多对多关联关系映射
【1】单向多对多如Category:Item=n:n。在关系数据模型中,是需要一个中间表Category-Item来维持这种关联关系的。该表中存放Category_ID和Item_ID。
hibernate多对多 对象关联关系映射 hibernate 多对多 关联关系 -
Hibernate 多对多关联
Hibernate 多对
hibernate function null string table -
spring mybatis多对多关联映射
orders→orderdetail:一个订单可以包括多个订单明细,因为一个订单可以购买多个商品,每个商品的购买信息在orderdetail中记录,是一对多关
mybatis 数据库 java List User