hibernate学习5之one-to-many双向关联

一对多双向关联映射:
* 在一一端的集合上使用<key>,在对方表中加入一个外键指向一一端
* 在多一端采用<many-to-one>

<!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="hibernate.connection.url">jdbc:mysql://localhost/hibernate_one2many_1</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">bjsxt</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		
		<mapping resource="com/bjsxt/hibernate/Classes.hbm.xml"/>
		<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


public class Student {
	private int id;	
	private String name;
	private Classes classes;//多对一,多个学生对应一个班级
	//setter,getter
}

import java.util.Set;

public class Classes {
	private int id;
	private String name;
	private Set students; //一对多
	//setter,getter
	
}

Student.hbm.xml:
<?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>
	<class name="com.bjsxt.hibernate.Student" table="t_student">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<many-to-one name="classes" column="classesid"/>
	</class>
</hibernate-mapping>

Classes.hbm.xml:
<?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.bjsxt.hibernate">
	<class name="Classes" table="t_classes">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="students" inverse="true" cascade="all">
			<key column="classesid"/>
			<one-to-many class="Student"/>
		</set>
	</class>
</hibernate-mapping>


注意:<key>标签指定的外键字段(这里是classesid)必须和<many-to-one>指定的外键字段(这里是classesid)一致,否则引用字段的错误

如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多的一端来维护关联关系。
加上inverse="true"这个属性,就可以强制在多的一端维护关系了。

关于inverse属性:
inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,
默认inverse为false,所以我们可以从”一“一端和”多“一端维护关联关系,
如果设置成inverse为true,则我们只能从多一端来维护关联关系

注意:inverse属性,只影响数据的存储,也就是持久化

inverse和cascade
* inverse是关联关系的控制方向
* cascade操作上的连锁反应

测试一下:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Session;

public class One2ManyTest extends TestCase {

	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Student student1 = new Student();
			student1.setName("10");
			session.save(student1);
			
			Student student2 = new Student();
			student2.setName("祖儿");
			session.save(student2);
			
			Set students = new HashSet();
			students.add(student1);
			students.add(student2);
			
			Classes classes = new Classes();
			classes.setName("尚学堂");
			classes.setStudents(students);
			
			//可以正确保存
			session.save(classes);
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	
	public void testSave2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Classes classes = new Classes();
			classes.setName("尚学堂");
			session.save(classes);
			
			Student student1 = new Student();
			student1.setName("10");
			student1.setClasses(classes);
			session.save(student1);
			
			Student student2 = new Student();
			student2.setName("祖儿");
			student2.setClasses(classes);
			session.save(student2);
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testSave3() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();

			Classes classes = new Classes();
			classes.setName("尚学堂");
			
			Student student1 = new Student();
			student1.setName("10");
			student1.setClasses(classes);
			
			Student student2 = new Student();
			student2.setName("祖儿");
			student2.setClasses(classes);
			
			Set students = new HashSet();
			students.add(student1);
			students.add(student2);
			
			classes.setStudents(students);
			
			//可以正确保存
			session.save(classes);
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Classes classes = (Classes)session.load(Classes.class, 1);
			System.out.println("classes.name=" + classes.getName());
			Set students = classes.getStudents();
			for (Iterator iter=students.iterator(); iter.hasNext();) {
				Student student = (Student)iter.next();
				System.out.println("student.name=" + student.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testLoad2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			System.out.println("student.classes.name=" + student.getClasses().getName());
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
}

你可能感兴趣的:(.net,Hibernate,mysql,xml,jdbc)