hibernate它5.many2one单向

hibernate它5.many2one单向

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

关系数据库表之间的关系:

1 正确 1 

1 正确 许多

许多 正确 许多

表间关系设计

基于主键关联

基于外键关联

基于中间表

1 对 1关系实现:

基于主键关联

基于外键关联

基于中间表

 

1 对 多关系实现:

基于外键关联

基于中间表

多 对 多关系实现:

基于中间表

面向对象实体关系

1 对 1 

1 对 多

多 对 多

方向 :

单向、双向

hibernate它5.many2one单向

CRUD:

many2one配置:

<?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.demo.model">

    <class name="Certificate" table="t_certificate">
        <id name="certificateId" column="certificate_id">
            <generator class="sequence"><param name="sequence">SEQ_T_CERTIFICATE</param></generator>
        </id>
        <property name="certificateName" column="certificate_name"/>
        <property name="certificateNo" column="certificate_no"/>
        <many-to-one name="student" column="student_id"></many-to-one>
    </class>
</hibernate-mapping>

<many-to-one name="student" column="student_id"></many-to-one>

增、删、改

@Test
	public void addTest() {
		Student student = new Student();
		student.setStudentName("王五");
		student.setAge(35);

		Certificate certificate1 = new Certificate();
		certificate1.setCertificateName("aa");
		certificate1.setCertificateNo("3a10001");
		certificate1.setStudent(student);
		Certificate certificate2 = new Certificate();
		certificate2.setCertificateName("bb");
		certificate2.setCertificateNo("3a10002");
		certificate2.setStudent(student);

		Session session = null;

		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			// 先保存one,再保存many
			session.save(student);
			session.save(certificate1);
			session.save(certificate2);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}

	}

	@Test
	public void deleteTest() {
		Student student = new Student();
		student.setStudentId(1);
		Certificate certificate1 = new Certificate();
		certificate1.setCertificateId(1);
		Certificate certificate2 = new Certificate();
		certificate2.setCertificateId(2);
		Session session = null;

		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.delete(certificate1);
			session.delete(certificate2);
			session.delete(student);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

	@Test
	public void updateTest() {
		Student student = new Student();
		student.setStudentId(2);
		student.setStudentName("赵四");
		Certificate certificate1 = new Certificate();
		certificate1.setCertificateId(3);
		certificate1.setCertificateName("cc");
		certificate1.setCertificateNo("s0001");
		certificate1.setStudent(student);
		Certificate certificate2 = new Certificate();
		certificate2.setCertificateId(4);
		certificate2.setCertificateName("dd");
		certificate2.setCertificateNo("s0002");
		certificate2.setStudent(student);
		Session session = null;

		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.update(certificate1);
			session.update(certificate2);
			session.update(student);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

getTest:

@Test
	public void updateTest() {
		Student student = new Student();
		student.setStudentId(2);
		student.setStudentName("赵四");
		Certificate certificate1 = new Certificate();
		certificate1.setCertificateId(3);
		certificate1.setCertificateName("cc");
		certificate1.setCertificateNo("s0001");
		certificate1.setStudent(student);
		Certificate certificate2 = new Certificate();
		certificate2.setCertificateId(4);
		certificate2.setCertificateName("dd");
		certificate2.setCertificateNo("s0002");
		certificate2.setStudent(student);
		Session session = null;

		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.update(certificate1);
			session.update(certificate2);
			session.update(student);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

结果:

Hibernate: select certificat0_.certificate_id as certific1_1_0_, certificat0_.certificate_name as certific2_1_0_, certificat0_.certificate_no as certific3_1_0_, certificat0_.student_id as student4_1_0_ from t_certificate certificat0_ where certificat0_.certificate_id=?
3
证书名称:cc
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=?

学员名称:赵四

Student(one)对象被延迟载入了,many2one中,
get支持延迟载入

loadTest:

@Test
	public void loadTest() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Certificate certificate = (Certificate) session.load(
					Certificate.class, 3);
			System.out.println(certificate.getCertificateId());
			System.out.println("证书名称:"+certificate.getCertificateName());
			System.out.println("学员名称:"+certificate.getStudent().getStudentName());
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

结果:

3
Hibernate: select certificat0_.certificate_id as certific1_1_0_, certificat0_.certificate_name as certific2_1_0_, certificat0_.certificate_no as certific3_1_0_, certificat0_.student_id as student4_1_0_ from t_certificate certificat0_ where certificat0_.certificate_id=?
证书名称:cc
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=?
学员名称:赵四


many与one都被延迟载入

补充表结构及实体类源代码:

t_student

hibernate它5.many2one单向

t_certificate:

hibernate它5.many2one单向

Student:

package com.demo.model;

import java.io.UnsupportedEncodingException;

/**学生信息双向
 * @author wobendiankun
 *2014-10-19 下午08:54:29
 */
public class Student {
	private int studentId ;
	private String studentName ;
	private int age;
	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 + "]";
	}
	
}

Certificate

package com.demo.model;

/**从业资格证书
 * @author wobendiankun
 *2014-10-25 上午11:43:21
 */
public class Certificate {
	/**
	 * 证书id
	 */
	private int certificateId ;
	/**
	 * 证书名称
	 */
	private String certificateName;
	/**
	 *证书编号
	 */
	private String certificateNo ;
	
	private Student student ;
	public int getCertificateId() {
		return certificateId;
	}
	public void setCertificateId(int certificateId) {
		this.certificateId = certificateId;
	}
	public String getCertificateName() {
		return certificateName;
	}
	public void setCertificateName(String certificateName) {
		this.certificateName = certificateName;
	}
	public String getCertificateNo() {
		return certificateNo;
	}
	public void setCertificateNo(String certificateNo) {
		this.certificateNo = certificateNo;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117521.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号