学生 和 课程就是一种多对多的关系映射,那么再hibernate中,对于多对多关系在怎么配置?
和一些注意的事项?简单测试下。
建表
实体
配置文件和映射
测试
1。使用oracle ,建表sql
create table students(
id number(7) primary key,
name nvarchar2(20),
age number(2)
)
create table course(
id number(7) primary key,
name nvarchar2(20),
time nvarchar2(20)
)
create table st_cou(
st_id references students(id),
cou_id references course(id),
primary key (st_id,cou_id)
)
2. students 实体
package com.hibernate.entity;
import java.util.HashSet;
import java.util.Set;
public class Students {
private Integer id;
private String name;
private Integer age;
private Set<Course> course = new HashSet<Course>();
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Course> getCourse() {
return course;
}
public void setCourse(Set<Course> course) {
this.course = course;
}
public Students(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Students() {
super();
}
@Override
public String toString() {
return "Students [id=" + id + ", name=" + name + ", age=" + age + ", course=" + course + "]";
}
}
3. course实体
package com.hibernate.entity;
import java.util.HashSet;
import java.util.Set;
public class Course {
private Integer id;
private String name;
private String time;
private Set<Students> students = new HashSet<Students>();
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;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Set<Students> getStudents() {
return students;
}
public void setStudents(Set<Students> students) {
this.students = students;
}
public Course(Integer id, String name, String time) {
super();
this.id = id;
this.name = name;
this.time = time;
}
public Course() {
super();
}
@Override
public String toString() {
return "Course [id=" + id + ", name=" + name + ", time=" + time + "]";
}
}
4.hibernate.cfg.xml 配置
<!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_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">zt</property>
<property name="hibernate.connection.password">zt</property>
<!-- 添加配置信息:hibernate 自身属性
dialect :方言,指定数据类型,hibernate可以针对不同的数据库做出相应的优化
-->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 为getCurrentSession增加配置 -->
<property name="current_session_context_class">thread</property>
<mapping resource="course.cfg.xml"/>
</session-factory>
</hibernate-configuration>
course.hbm.xmc 映射配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.entity">
<class name="Students" table="students">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="age"/>
<property name="name"/>
<!-- name:是实体中关联的另一方属性
cascade:是级联级别
table:是 学生和 课程中间关联的那张表
下面course配置和students配置是一样的。
-->
<set name="course" cascade="all" table="st_cou">
<!-- columun:是关系表中的自己的维护字段 -->
<key column="st_id"></key>
<!-- class:是关系对方的实体
column:是关系对方的维护字段
-->
<many-to-many class="Course" column="cou_id"/>
</set>
</class>
<class name="Course" table="course">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"/>
<property name="time"/>
<set name="students" cascade="all" table="st_cou">
<key column="cou_id"></key>
<many-to-many class="Students" column="st_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
5. 测试
查询和更新测试略
查询直接查就可以了,更新查询后,修改值后,更新就可以了。
插入测试,级联插入
@Test
public void inset(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Course course = new Course(null, "地理", "面貌");
Students students = new Students(null, "蛋蛋", 18);
students.getCourse().add(course);
// course.getStudents().add(students);这句不用写
session.save(students);
tx.commit();
}
多对多中,插入,只要乙方进行维护就可以,如果二方都写
students.getCourse().add(course);
course.getStudents().add(students);
那么插入中,再关联表中,由于乙方已经维护,此时已经存在关联关系,这时候,另一方已进行维护
那么由于已经存在关联关系,这时候会报 违反唯一约束条件
删除测试
@Test
public void delete(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Students students = (Students)session.get(Students.class, 1);
session.delete(students);
tx.commit();
}
执行之后,进入数据库查询,发现,只要是在关联表中存在的,相关学生和课程都会被删除。
配置中,我们配置了级联级别是 all 这中权限过大,慎用。级联级别修改为 save-update
6.注意
1.实体中 我们写关系一方有
private Set<Course> course = new HashSet<Course>();
为什么 new HashSet<Course>();
当我们进行单个插入表时,如果没有new HashSet<Course>();
Students students = (Students)session.get(Students.class, 1);
Course course = new Course(null, "地理", "面貌");
这里的course.getStudents() 是个null 而用null对象.add 会是空指针
course.getStudents().add(students);
2. 级联关系
在关联关系中,要慎用关系,以免对另一方数据操作损失。