hihernate一对多关联映射(双向Classes<----->Student)
一对多双向关联映射:
* 在一一端的集合上使用<key>,在对方表中加入一个外键指向一的一端
* 在多一端采用<many-to-one>
注意:<key>标签指定的外键字段必须和<many-to-one>指定的外键字段一致,否则引用字段的错误
如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多
的一端来维护关联关系
关于inverse属性:
inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,
默认inverse为false,所以我们可以从”一“一端和”多“一端维护关联关系,
如果设置成inverse为true,则我们只能从多一端来维护关联关系
注意:inverse属性,只影响数据的存储,也就是持久化
inverse和cascade
* inverse是关联关系的控制方向
* cascade操作上的连锁反应
1.实体类
2.映射文件
3.hibernate配置文件
4.测试类
一对多双向关联映射:
* 在一一端的集合上使用<key>,在对方表中加入一个外键指向一的一端
* 在多一端采用<many-to-one>
注意:<key>标签指定的外键字段必须和<many-to-one>指定的外键字段一致,否则引用字段的错误
如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多
的一端来维护关联关系
关于inverse属性:
inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,
默认inverse为false,所以我们可以从”一“一端和”多“一端维护关联关系,
如果设置成inverse为true,则我们只能从多一端来维护关联关系
注意:inverse属性,只影响数据的存储,也就是持久化
inverse和cascade
* inverse是关联关系的控制方向
* cascade操作上的连锁反应
1.实体类
package com.yx.zzg.pojo;
public class Student {
private int id;
private String name;
private Classess clasess;
public Classess getClasess() {
return clasess;
}
public void setClasess(Classess clasess) {
this.clasess = clasess;
}
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;
}
}
package com.yx.zzg.pojo;
import java.util.Set;
public class Classess {
private int id;
private String name;
private Set<Student> student;
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = 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;
}
}
2.映射文件
<?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.yx.zzg.pojo">
<class name="Student" table="t_student">
<id name="id">
<!-- 指定主键生成策略 -->
<generator class="native" />
</id>
<property name="name" />
<!-- 指定column属性的值与Classess.hbm.xml中的key的外键保持一致,否则会引起字段错误 -->
<many-to-one name="clasess" column="classessid" />
</class>
</hibernate-mapping>
<?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.yx.zzg.pojo">
<class name="Classess" table="t_classess">
<id name="id">
<!-- 指定主键生成策略 -->
<generator class="native" />
</id>
<property name="name" />
<set name="student" inverse="true" cascade="save-update">
<key column="classessid" />
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping>
3.hibernate配置文件
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate_one2many_1</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否显示执行的sql语句 -->
<property name="show_sql">true</property>
<!-- 显示执行的sql语句是否格式化 -->
<property name="format_sql">true</property>
<!-- 是否自动创建表,参数详见HIBERNATE_HOME/etc/hibernate.properties文件 -->
<!--
<property name="hibernate.hbm2ddl.auto">create</property>
-->
<mapping resource="com/yx/zzg/pojo/Student.hbm.xml" />
<mapping resource="com/yx/zzg/pojo/Classess.hbm.xml" />
</session-factory>
</hibernate-configuration>
4.测试类
package com.yx.zzg.test;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.yx.zzg.pojo.Classess;
import com.yx.zzg.pojo.Student;
import com.yx.zzg.util.HibernateUtil;
public class TestUser extends TestCase {
/**
* 使用一的一端来维护,不设置inverse及cascade属性
*
* 先保存Student,再保存Classess,然后再更新Student
*
* 结果:会发出update语句
*/
public void testSave1() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Student s1 = new Student();
s1.setName("zzg");
session.save(s1);
Student s2 = new Student();
s2.setName("zlh");
session.save(s2);
Set<Student> students = new HashSet<Student>();
students.add(s1);
students.add(s2);
Classess clas = new Classess();
clas.setName("java");
clas.setStudent(students);
session.save(clas);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
/**
* 使用多的一端来维护,不设置inverse及cascade属性
*
* 先保存Classess,在保存Student
*
* 结果:不会发出update语句
*/
public void testSave2() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Classess clas = new Classess();
clas.setName("java");
session.save(clas);
Student s1 = new Student();
s1.setName("zzg");
s1.setClasess(clas);
session.save(s1);
Student s2 = new Student();
s2.setName("zlh");
s2.setClasess(clas);
session.save(s2);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
/**
* 使用一的一端来维护并且在set标签上设置inverse="true"
*
* 先保存Student,再保存Classess.
*
* 结果:不会发出update语句,是因为设置inverse=
* "true后会反转到多的一端来维护,但表classess中的外键classessid的值为NULL
*/
public void testSave3() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Student s1 = new Student();
s1.setName("zzg");
session.save(s1);
Student s2 = new Student();
s2.setName("zlh");
session.save(s2);
Set<Student> students = new HashSet<Student>();
students.add(s1);
students.add(s2);
Classess clas = new Classess();
clas.setName("java");
clas.setStudent(students);
session.save(clas);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
/**
* 使用一的一端来维护并且在set标签上设置inverse="true"并且在set标签上设置cascade="save-update"
*
* 结果:会正确保存并且不会发出update语句,是因为设置inverse=
* "true后会反转到多的一端来维护,而且在保存Student之前会先保存Classess
*/
public void testSave4() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Classess clas = new Classess();
clas.setName("java");
Student s1 = new Student();
s1.setName("zzg");
s1.setClasess(clas);
Student s2 = new Student();
s2.setName("zlh");
s2.setClasess(clas);
Set<Student> students = new HashSet<Student>();
students.add(s1);
students.add(s2);
clas.setStudent(students);
session.save(clas);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
/**
* 从一的一端加载
*/
public void testLoad1() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Classess clas = (Classess) session.load(Classess.class, 1);
System.out.println("clas.name=" + clas.getName());
Set<Student> students = clas.getStudent();
for (Iterator<Student> iter = students.iterator(); iter.hasNext();) {
Student s = iter.next();
System.out.println("Student.name=" + s.getName());
}
tx.commit();
} catch (HibernateException e) {
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
/**
* 从多的一端加载
*/
public void testLoad2() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Student s=(Student)session.load(Student.class, 1);
System.out.println("s.name="+s.getName());
System.out.println("s.classess.name="+s.getClasess().getName());
tx.commit();
} catch (HibernateException e) {
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
}