hibernate之6.one2many单向

表结构:



实体类图:



CRUD:

Student:

package com.demo.model;

import java.io.UnsupportedEncodingException;
import java.util.Set;

/**学生信息
 * @author wobendiankun
 *2014-10-19 下午08:54:29
 */
public class Student {
	private int studentId ;
	private String studentName ;
	private int age;
	private Set<Certificate> certificates ;
	public int getStudentId() {
		return studentId;
	}
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		String str="";
		if(studentName!=null){
			try {
				str=new String(studentName.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return "Student [studentId=" + studentId + ", studentName="
				+ str + ", age=" + age + "]";
	}
	public Set<Certificate> getCertificates() {
		return certificates;
	}
	public void setCertificates(Set<Certificate> certificates) {
		this.certificates = certificates;
	}
	
}

配置:

<?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.demo.model.Student" table="t_student">
		<id name="studentId" column="student_id">
			<generator class="sequence">
				<param name="sequence">SEQ_T_STUDENT</param>
			</generator>
		</id>
		<property name="studentName" column="student_name" />
		<property name="age" />
		
		<set name="certificates">
			<key column="student_id"></key>
			<one-to-many class="com.demo.model.Certificate"/>
		</set>
	</class>
</hibernate-mapping>

说明:

<set name="certificates">

<key column="student_id"></key>

<one-to-many class="com.demo.model.Certificate"/>

</set>

name:Student类的certificates属性名

<key column="student_id"></key> :关联字段名称

<one-to-many class="com.demo.model.Certificate"/>  : Set元素的类全名


add:

@Test
	public void addTest() {
		Certificate certificate1 = new Certificate();
		certificate1.setCertificateName("aa");
		certificate1.setCertificateNo("3a10001");
		Certificate certificate2 = new Certificate();
		certificate2.setCertificateName("bb");
		certificate2.setCertificateNo("3a10002");
		Set<Certificate> set=new HashSet<Certificate>();
		set.add(certificate1);
		set.add(certificate2);
		
		Student student = new Student();
		student.setStudentName("钱六");
		student.setAge(35);
		student.setCertificates(set);
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.save(student);
			session.save(certificate1);
			session.save(certificate2);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}

	}


发出的SQL:

Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?)
Hibernate: insert into t_certificate (certificate_name, certificate_no, certificate_id) values (?, ?, ?)
Hibernate: insert into t_certificate (certificate_name, certificate_no, certificate_id) values (?, ?, ?)
Hibernate: update t_certificate set student_id=? where certificate_id=?
Hibernate: update t_certificate set student_id=? where certificate_id=?

从两条update语句中,我们发现此时的关联字段由one(Student)的一方来维护,这个明显不合适


loadTest:

@Test
	public void loadTest() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Student student =(Student) session.load(Student.class, 23);
			System.out.println("姓名:"+student.getStudentName());
			System.out.println("数量:"+student.getCertificates().size());
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
		
	}


发出的SQL:

Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
姓名:钱六
Hibernate: select certificat0_.student_id as student4_0_1_, certificat0_.certificate_id as certific1_1_, certificat0_.certificate_id as certific1_1_0_, certificat0_.certificate_name as certific2_1_0_, certificat0_.certificate_no as certific3_1_0_ from t_certificate certificat0_ where certificat0_.student_id=?
数量:2


loadLazyExtra:

在set标签中添加属性:lazy="extra"

<?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.demo.model.Student" table="t_student">
		<id name="studentId" column="student_id">
			<generator class="sequence">
				<param name="sequence">SEQ_T_STUDENT</param>
			</generator>
		</id>
		<property name="studentName" column="student_name" />
		<property name="age" />
		
		<set name="certificates" lazy="extra"><!-- lazy="extra" -->
			<key column="student_id"></key>
			<one-to-many class="com.demo.model.Certificate"/>
		</set>
	</class>
</hibernate-mapping>

运行代码:

@Test
	public void loadLazyExtraTest() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Student student =(Student) session.load(Student.class, 23);
			System.out.println("姓名:"+student.getStudentName());
			System.out.println("数量:"+student.getCertificates().size());
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}


发出的SQL:

Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
姓名:钱六
Hibernate: select count(certificate_id) from t_certificate where student_id =?
数量:2


loadAssociatesTest:

@Test
	public void loadAssociatesTest() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Student student =(Student) session.load(Student.class, 23);
			System.out.println("姓名:"+student.getStudentName());
			for(Certificate c:student.getCertificates()){
				System.out.println("编号:"+c.getCertificateNo());
			}
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}


发出的SQL:

Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
姓名:钱六
Hibernate: select certificat0_.student_id as student4_0_1_, certificat0_.certificate_id as certific1_1_, certificat0_.certificate_id as certific1_1_0_, certificat0_.certificate_name as certific2_1_0_, certificat0_.certificate_no as certific3_1_0_ from t_certificate certificat0_ where certificat0_.student_id=?
编号:3a10001
编号:3a10002


第二条查询语句用于查询关联对象,其实我们可以通过一条sql语句查询关联对象,如下:

select * from  t_student t1
inner join   t_certificate t2 on t2.student_id=t1.student_id
where t1.student_id=23;

后面再进行优化


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。