如teacher表和student表是多对多关系,下面是他们的对象及Xml文件
Student.java
package com.landray.domain;
import java.util.HashSet;
import java.util.Set;
public class Student {
private int id;
private String name;
private Set<Teacher> teacher = new HashSet<Teacher>();
public Set<Teacher> getTeacher() {
return teacher;
}
public void setTeacher(Set<Teacher> teacher) {
this.teacher = teacher;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", teacher="+teacher+"";
}
}
Teacher.java
package com.landray.domain;
import java.util.HashSet;
import java.util.Set;
public class Teacher {
private int id;
private String name;
private Set<Student> student = new HashSet<Student> ();
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> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
}
student.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">
<!-- ORM元数据 表对象关系映射文件
package : 配置该配置文件中类所在的包. -->
<hibernate-mapping package="com.landray.domain" >
<class name="Student" table="t_student" >
<id name="id" column="id" >
<generator class="native"></generator>
</id>
<property name="name" column="name" ></property>
<!-- 多对多关系 -->
<!--
set 表达集合
name: 集合的属性名
table:多对多中间表的表名
key 表达外键
column:引用我的外键名
many-to-many 表达多对多
class : 集合引用方的类型
column:对方在中间表的外键名
-->
<set name="teacher" table="t_student_course" fetch="join" inverse="false" cascade="all" >
<key column="sid" ></key>
<many-to-many class="Teacher" column="tid" ></many-to-many>
</set>
</class>
</hibernate-mapping>
teacher.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">
<!-- ORM元数据 表对象关系映射文件
package : 配置该配置文件中类所在的包. -->
<hibernate-mapping package="com.landray.domain" >
<class name="Teacher" table="t_teacher" >
<id name="id" column="id" >
<generator class="increment"></generator>
</id>
<property name="name" column="name" ></property>
<!-- 多对多关系 -->
<!--
set 表达集合
name: 集合的属性名
table:多对多中间表的表名
key 表达外键
column:引用我的外键名
many-to-many 表达多对多
class : 集合引用方的类型
column:对方在中间表的外键名
-->
<set name="student" table="t_student_course" inverse="true" cascade="save-update" >
<key column="tid" ></key>
<many-to-many class="Student" column="sid" ></many-to-many>
</set>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?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 元素用于配置Hibernate中的属性
键:值
-->
<!-- hibernate.connection.driver_class : 连接数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- hibernate.connection.username : 连接数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- hibernate.connection.password : 连接数据库的密码 -->
<property name="hibernate.connection.password">6677188</property>
<!-- hibernate.connection.url : 连接数据库的地址,路径 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/beijingstud</property>
<!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- format_sql: 打印sql语句前,会将sql语句先格式化 -->
<property name="format_sql">true</property>
<!-- hbm2ddl.auto: 生成表结构的策略配置
update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构.
如果存在表结构,并且表结构与实体一致,那么不做修改
如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.
create(很少):无论是否存在表结构.每次启动Hibernate都会重新创建表结构.(数据会丢失)
create-drop(极少): 无论是否存在表结构.每次启动Hibernate都会重新创建表结构.每次Hibernate运行结束时,删除表结构.
validate(很少):不会自动创建表结构.也不会自动维护表结构.Hibernate只校验表结构. 如果表结构不一致将会抛出异常.
-->
<property name="hbm2ddl.auto">update</property>
<!-- 数据库方言配置
org.hibernate.dialect.MySQLDialect (选择最短的)
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- hibernate.connection.autocommit: 事务自动提交 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 引入ORM 映射文件
填写src之后的路径
-->
<mapping resource="com/landray/domain/Banji.hbm.xml"/>
<mapping resource="com/landray/domain/Student.hbm.xml"/>
<mapping resource="com/landray/domain/Teacher.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试方法
package com.landray.manytomany;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import com.landray.domain.Banji;
import com.landray.domain.Student;
import com.landray.domain.Teacher;
public class Demo1 {
@Test
public void insert(){
//1 读取配置文件
Configuration conf = new Configuration().configure();
//2 根据配置 创建Factory
SessionFactory sessionFactory = conf.buildSessionFactory();
//3 通过获得操作数据库的session对象
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
//------------------------------------------------
Student student1 = new Student();
student1.setName("李华");
Student student2 = new Student();
student2.setName("李娜");
Teacher teacher1 = new Teacher();
teacher1.setName("王明");
Teacher teacher2 = new Teacher();
teacher2.setName("王亮");
student1.getTeacher().add(teacher1);
student1.getTeacher().add(teacher2);
student2.getTeacher().add(teacher1);
student2.getTeacher().add(teacher2);
session.save(student1);
session.save(student2);
ts.commit();
session.close();
}
@Test
public void select(){
//1 读取配置文件
Configuration conf = new Configuration().configure();
//2 根据配置 创建Factory
SessionFactory sessionFactory = conf.buildSessionFactory();
//3 通过获得操作数据库的session对象
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
//------------------------------------------------
Student student = (Student) session.get(Student.class, 1);
//Set sets = student.getTeacher();
System.out.println(student.toString());
//------------------------------------------------
ts.commit();
session.close();
}
注意的点:
1、多对多中,为了在select方法中检查方便,我们一般在对象中会重写toString方法,但是,在多对多的关系中,切记只能完整的重写一边的toString方法,一旦两边都完整的写了toString方法,会出现死循环,知道提示栈溢出。 解决方法我想到两种,第一种,只完整的写一边的toString方法,另一边则把关联的那部分去掉。如在Teacher.java中我就把Student去掉了。public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
},只保留了id和姓名。第二种方法,不用toString方法来显示,直接通过get方法来把数据输出来。如上面的我们就可以直接把从select 方法中获得的student对象的set <Teacher> teacher 用get方法来显示出来。
2.一对多中,最好是在多的一方设置级联,主外键关联关系只是作为一种连接,方便查找并没有特定的裙带关系,你删除子表的数据是否会删除掉主表的数据这取决于你的业务需求,hibernate是支持级联的,如果你设置了级联删除那么就会连带主表的记录一并删除