一对多关联映射

hihernate一对多关联映射(单向Classes----->Student)

一对多关联映射利用了多对一关联映射原理

多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一(当加载多的一端时可以加载一的一端)
一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多(当加载一的一端时可以加载多的一端)

也就是说一对多和多对一的映射策略是一样的,只是站的角度不同

在一的一端维护关系的缺点:
           * 如果将t_student表里的classesid字段设置为非空,则无法保存
           * 因为不是在student这一端维护关系,所以student不知道是哪个班的,所以需要发出多余的update语句来更新关系
import java.util.Set;

public class Classes {
	
	private int id;
	
	private String name;
	
	private Set<Student> 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;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

	public Set<Student> getStudents() {
		return students;
	}
}



public class Student {
	
	private int id;
	
	private String 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;
	}
}
//Classes.hbm.xml
<hibernate-mapping package="com.bjsxt.hibernate">
	<class name="Classes" table="t_classes">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<set name="students"><!-- 在Student表中增加classesid属性-->
			<key column="classesid"/><!-- Student以classesid作为外键指向classes -->
			<one-to-many class="Student"/><!-- 指定集合中的元素类型 -->
		</set>
	</class>
</hibernate-mapping>


//Student.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Student" table="t_student">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
	</class>
</hibernate-mapping>
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);  //save对象,transient状态转变为persistent

			Student student2 = new Student();
			student2.setName("祖儿");
			session.save(student2);

			Set<Student> students = new HashSet<Student>();// 先建学生,再将学生放入集合中
			students.add(student1);
			students.add(student2);

			Classes classes = new Classes();// 将学生集合加入班级
			classes.setName("尚学堂");
			classes.setStudents(students);

			// 可以正确保存
			session.save(classes);
			/*sql语句:会发出多余的update语句,如果student表中设置为非空则无法存存储
			 * Hibernate: insert into t_student (name, id) values (?, ?)
			 * Hibernate: insert into t_student (name, id) values (?, ?)
			 * Hibernate: insert into t_classes (name, id) values (?, ?)
			 * Hibernate: update t_student set classesid=? where id=?
			 * Hibernate: update t_student set classesid=? where id=?
			 */
			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, 3);// get、load根据主键加载
			System.out.println("classes.name=" + classes.getName());
			Set<Student> students = classes.getStudents(); // 加载班级学生的集合
			for (Iterator<Student> iter = students.iterator(); iter.hasNext();) {
				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);
		}
	}
}



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操作上的连锁反应
public class Student {
	
	private int id;
	
	private String name;
	
	private Classes classes;

	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;
	}

	public Classes getClasses() {
		return classes;
	}

	public void setClasses(Classes classes) {
		this.classes = classes;
	}
}



import java.util.Set;

public class Classes {
	
	private int id;
	
	private String name;
	
	private Set<Student> 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;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

	public Set<Student> getStudents() {
		return students;
	}
	
}
//Student.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Student" table="t_student">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<many-to-one name="classes" column="classesid"/>
		<!-- 在表中增加属性字段classesid,指向classes -->
	</class>
</hibernate-mapping>


//Classes.hbm.xml
<hibernate-mapping package="com.bjsxt.hibernate">
	<class name="Classes" table="t_classes">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<set name="students" inverse = "true" cascade = "all"><!-- 在Student表中增加classesid属性-->
			<key column="classesid"/><!-- Student以classesid作为外键指向classes -->
			<one-to-many class="Student"/><!-- 指定集合中的元素类型 -->
		</set>
	</class>
</hibernate-mapping>
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);  //save对象,transient状态转变为persistent

			Student student2 = new Student();
			student2.setName("祖儿");
			session.save(student2);

			Set<Student> students = new HashSet<Student>();// 先建学生,再将学生放入集合中
			students.add(student1);
			students.add(student2);

			Classes classes = new Classes();// 将学生集合加入班级
			classes.setName("尚学堂");
			classes.setStudents(students);

			// 可以正确保存
			session.save(classes);
			/*sql语句:会发出多余的update语句,如果student表中设置为非空则无法存存储
			 * Hibernate: insert into t_student (name, id) values (?, ?)
			 * Hibernate: insert into t_student (name, id) values (?, ?)
			 * Hibernate: insert into t_classes (name, id) values (?, ?)
			 * Hibernate: update t_student set classesid=? where id=?
			 * Hibernate: update t_student set classesid=? where id=?
			 */
			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);
			/*
			 * Hibernate: insert into t_classes (name, id) values (?, ?)
			 * Hibernate: insert into t_student (name, classesid, id) values (?, ?, ?)
			 * Hibernate: insert into t_student (name, classesid, id) values (?, ?, ?)
			 */
			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<Student> students = new HashSet<Student>();
			students.add(student1);
			students.add(student2);
			
			classes.setStudents(students);
			
			//可以正确保存,只设置 inverse = "true"  
			//只发出Hibernate: insert into t_classes (name, id) values (?, ?)
			session.save(classes);
			/* 设置inverse = "true" cascade = "all"
			 * Hibernate: insert into t_classes (name, id) values (?, ?)
			 * Hibernate: insert into t_student (name, classesid, id) values (?, ?, ?)
			 * Hibernate: insert into t_student (name, classesid, id) values (?, ?, ?)
			 */
			/*只设置cascade = "all"
			 * Hibernate: insert into t_classes (name, id) values (?, ?)
			 * Hibernate: insert into t_student (name, classesid, id) values (?, ?, ?)
			 * Hibernate: insert into t_student (name, classesid, id) values (?, ?, ?)
			 * Hibernate: update t_student set classesid=? where id=?
			 * Hibernate: update t_student set classesid=? where id=?
			 */
			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, 3);// get、load根据主键加载
			System.out.println("classes.name=" + classes.getName());
			Set<Student> students = classes.getStudents(); // 加载班级学生的集合
			for (Iterator<Student> iter = students.iterator(); iter.hasNext();) {
				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);
		}
	}
}

你可能感兴趣的:(sql,Hibernate,xml,JUnit)