类图如下:
在关系模型中,同多对一一样,还是在Student表中有一个外键指向Class表。
在配置的时候需要注意了,Student配置文件不需要进行额外的配置了,需要进行额外配置的是MyClass的配置文件,需要通过set标签配置,这里set标签也是有两个作用的,一为数据库服务,第二位程序服务。
MyClass.java
package com.org.model;
import java.util.HashSet;
import java.util.Set;
public class MyClass {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = 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;
}
}
Student.java
package com.org.model;
public class Student {
private int id;
private String name;
public Student(){}
public Student(String name){
this.name = name;
}
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;
}
}
MyClass.hbm.xml
<?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.org.model"> <class name="MyClass" table="class"> <id name="id"> <generator class="native"/> </id> <property name="name" not-null="true"/> <!-- set标签中的name为MyClass类中多方对应的属性名 key标签中的column为多方数据库表中的外键字段--> <set name="students" cascade="save-update"> <key column="class_id" not-null="true"></key> <one-to-many class="Student"/> <!-- class表明set中放的是什么类型的集合 --> </set> </class> </hibernate-mapping>Student.hbm.xml
<?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.org.model"> <class name="Student" table="student"> <id name="id"> <generator class="native"/> </id> <property name="name" column="name" not-null="true"/> </class> </hibernate-mapping>测试类HibernateTest.java
package com.org.model.test;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.org.model.MyClass;
import com.org.model.Student;
import com.org.util.HibernateUtil;
public class HibernateTest {
@Test
public void testSave1() {
Session session = null;
Transaction tx = null;
try {
// 拿到session
session = HibernateUtil.getSession();
System.out.println(session);
// 开启事务
tx = session.beginTransaction();
// 给实体赋值
MyClass class1 = new MyClass();
class1.setName("09001");
Student st1 = new Student("aaa");
Student st2 = new Student("bbb");
Student st3 = new Student("ccc");
HashSet<Student> students = new HashSet<Student>();
students.add(st1);
students.add(st2);
students.add(st3);
class1.setStudents(students);
session.save(class1);
// 提交事务
tx.commit();
} catch (Exception e) {
// 打印堆栈信息
e.printStackTrace();
// 事务回滚
if (tx != null) {
tx.rollback();
}
}
}
}
由于配置了级联关系,Hibernate在插入班级数据(一方)时,会把它所关联的学生数据(多方)也保存到数据库中。
缺点:对象之间的关联关系由一方来维护,在插入一方数据时会更新多方的与这个“一”的对象有关联的对象。导致频繁访问数据库。
测试时,控制台输出的sql语句:
Hibernate:
insert
into
class
(name)
values
(?)
Hibernate:
insert
into
student
(name, class_id)
values
(?, ?)
Hibernate:
insert
into
student
(name, class_id)
values
(?, ?)
Hibernate:
insert
into
student
(name, class_id)
values
(?, ?)
Hibernate:
update
student
set
class_id=?
where
id=?
Hibernate:
update
student
set
class_id=?
where
id=?
Hibernate:
update
student
set
class_id=?
where
id=?