类图:
数据库中表的关系同样还只是外键约束,这种关系交给了“多”这一方来维护。
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;
private MyClass classes;
public MyClass getClasses() {
return classes;
}
public void setClasses(MyClass classes) {
this.classes = classes;
}
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" inverse="true"> <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"/> <!-- name指Student类中对应一方的属性名 cascade属性指定级联操作 class指一方的类名 column中的name表示关联的外键 not-null表示此外键不能为空 --> <many-to-one name="classes" class="MyClass" cascade="save-update"> <column name="class_id" not-null="true"></column> </many-to-one> </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();
// 开启事务
tx = session.beginTransaction();
// 给实体赋值
MyClass class1 = new MyClass();
class1.setName("09002");
Student st1 = new Student("aaa");
Student st2 = new Student("bbb");
Student st3 = new Student("ccc");
st1.setClasses(class1);
st2.setClasses(class1);
st3.setClasses(class1);
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();
}
}finally{
HibernateUtil.closeSession(session);
}
}
}
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
(?, ?)
因为多方配置了级联,所以插入班级的时候也会插入与它关联的学生。如果多方不配置级联,那么只会插入班级数据,并不会插入与它关联的学生记录。