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

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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关联图谱位于反欺诈技术金字塔模型什么层_知识图谱的应用场景

    关联图谱位于反欺诈技术金字塔模型什么层_知识图谱的应用场景关系图谱概要随着近几年互联网金融的发展,玲琅满目的信贷产品早已被羊毛党盯上,层出不穷的营销活动更是让欺诈分子有了可乘之机,伪造资料、恶意注册大量虚假账号、团伙包装、刷单、抢红包、套返利等等,他们的欺诈技术手段也越来越高明(群控、云控),成本也越来越低。为了限制这些欺诈用户,信贷机构通过建立反欺诈团队和风控防范体系,使用专家规则和预测模型来拦截欺诈份子。但是道高一尺魔高一丈,再严密的规则也…

    2022年4月19日
    98
  • Mybatis源码解析一(SqlSessionFactory和SqlSession的获取)

    Mybatis源码解析一(SqlSessionFactory和SqlSession的获取)一、SqlSessionFactorySqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像;SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得;SqlSessionFactoryBuilder从XML配置文件或一个预先定制的Configuration的实例构建出SqlS…

    2022年5月29日
    25
  • pycharm安装第三方库失败_pycharm怎么安装python库

    pycharm安装第三方库失败_pycharm怎么安装python库pycharm安装第三方库首先启动pycharm随后通过快捷键进入setting界面,快捷键为ctrl+Als+S点击右上角的加号,添加第三方库在安装之前,首先点击ManageRepositories增加一些国内的镜像源,包括豆瓣http://pypi.douban.com/simple/阿里的,http://mirrors.aliyun.com/pypi/simple/https://pypi…

    2022年8月28日
    2
  • 计算机最炫民族风教案,辽师大版信息技术四下第一单元第6课《最炫民族风》教案7.docx…[通俗易懂]

    计算机最炫民族风教案,辽师大版信息技术四下第一单元第6课《最炫民族风》教案7.docx…[通俗易懂]辽师大版信息技术四下第一单元第6课《最炫民族风》教案7.docx文档编号:536850文档页数:5上传时间:2019-01-13文档级别:文档类型:docx文档大小:7.00MB课课题题美丽的辽宁我的家美丽的辽宁我的家–WordWord软件的简单应用软件的简单应用66、、最炫民族风最炫民族风教学目标教学目标1、学会结合文章的内容进行分栏;2、学会设置页面颜色与页面效果;3、…

    2022年9月22日
    0
  • pycharm鼠标滚动控制字体大小_pycharm窗口放大

    pycharm鼠标滚动控制字体大小_pycharm窗口放大1、放大页面方法第一步:打开file里面的setting,然后打开Keymap,再搜索框中输入increase,点击increaseFontSize,双击AddMouseShortcut(先不用点OK)第二步:点击AddMouseShortcut弹出下面对话框,然后按住ctrl并向上滚动鼠标滑轮,将变成第二个对话框,点击OK;第三步:显示下面页面表示设置放大成功,点击OK即可。2、缩小页面方法与上面方法类似,将increase变成decrease输入即可;…

    2022年8月26日
    2
  • python实现二叉树层序遍历(逐层打印二叉树)「建议收藏」

    python实现二叉树层序遍历(逐层打印二叉树)「建议收藏」题目要求给定一个二叉树,要求从上往下逐层打印该二叉树节点的值,每层从左往右打印。解题思路——广度优先遍历实际上就是广度优先遍历,借助一个队列(这里用数组代替)就可以实现:1、先将root节点加入队列2、队列不为空时取队列首节点3、打印节点的值,然后将该节点的左、右子节点先后加入队尾(核心步骤,广度优先体现在这)4、回到2,直到队列为空该方法对满二叉树和非满二叉树都符合题目要求。…

    2022年5月21日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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