Hibernate一对一关系映射
Hibernate提供了两种一对一映射关联关系的方式:
1)按照外键映射
2)按照主键映射
下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映射方式分别完成以下持久化操作
(1)保存员工档案的同时分配给员工一个账号
(2)加载员工档案的同时加载账号信息
一:按照外键映射
HibernateUtil工具类(用于获取session和关闭session)
package cn.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { //初始化一个ThreadLocal对象,有get和set方法 private static final ThreadLocal<Session> sessionTL=new ThreadLocal<Session>(); private static Configuration configuration; private final static SessionFactory sessionFactory; static{ configuration=new Configuration().configure(); sessionFactory=configuration.buildSessionFactory(); } //获得session对象 public static Session currentSession() { //sessionTL的get方法根据当前线程返回其对应的线程内部变量,即Session对象,多线程情况下共享数据库连接是不安全的。 //ThreadLocal保证了每个线程都有自己的session对象 Session session=(Session)sessionTL.get(); if (session==null) { session=sessionFactory.openSession(); sessionTL.set(session); } return session; } //关闭session对象 public static void closeSession() { Session session=(Session)sessionTL.get(); sessionTL.set(null); session.close(); } }
创建实体类Users1和Resume1
Users1:
package cn.entity_fk; /** * 员工类 * * * */ public class Users1 { private Integer userid;//用户编号 private String username;//名称 private String userpass;//密码 private Resume1 resume1;//档案对象 public Users1() { } public Users1(String username, String userpass) { this.username = username; this.userpass = userpass; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public Resume1 getResume1() { return resume1; } public void setResume1(Resume1 resume1) { this.resume1 = resume1; } }
Resume1:
package cn.entity_fk; /** * 档案类 * * @time * @author Happy * */ public class Resume1 { private Integer resid; //档案编号 private String resname; //档案名称 private String rescardno;//编号 private Users1 users1; //隶属的用户(员工) public Resume1() { } public Resume1( String resname, String rescardno) { this.resname = resname; this.rescardno = rescardno; } public Integer getResid() { return resid; } public void setResid(Integer resid) { this.resid = resid; } public String getResname() { return resname; } public void setResname(String resname) { this.resname = resname; } public String getRescardno() { return rescardno; } public void setRescardno(String rescardno) { this.rescardno = rescardno; } public Users1 getUsers1() { return users1; } public void setUsers1(Users1 users1) { this.users1 = users1; } }
创建配置文件Users1.hbm.xml和Resume1.hbm.xml
Users1.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity_fk"> <class name="Users1" table="USERS1"> <!-- 主键的配置 --> <id name="userid" column="USERID" > <!-- 由后台数据库生成主键:默认生成的序列名称为:Hibernate_Sequence --> <generator class="native"></generator> </id> <property name="username" column="USERNAME" type="string"></property> <property name="userpass" column="USERPASS" type="string"></property> <!-- 配置一对对,外键方式的关联 property-ref:通过Resume1 的users1的属性,建立了从users1到Resume1的对象的关联! --> <one-to-one name="resume1" class="Resume1" property-ref="users1"></one-to-one> </class> </hibernate-mapping>
Resume1.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity_fk"> <class name="Resume1" table="RESUME1"> <id name="resid" column="RESID" > <generator class="native"></generator> </id> <property name="resname" column="RESNAME" type="string"></property> <property name="rescardno" column="RESCARDNO" type="string"></property> <many-to-one name="users1" class="Users1" cascade="all" column="RESUSERID" unique="true"></many-to-one> </class> </hibernate-mapping>
hibernate.cfg.xml大配置文件:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">***</property> <property name="connection.password">***</property> <!-- SQL dialect (SQL 方言)--> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- Echo all executed SQL to stdout 在控制台打印后台的SQL语句--> <property name="show_sql">true</property> <!-- 格式化显示SQL --> <property name="format_sql">true</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- Enable Hibernate's automatic session context management 指定当前session范围和上下文--> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>--> <mapping resource="cn/zhang/entity/Resume1.hbm.xml" /> <mapping resource="cn/zhang/entity/Users1.hbm.xml" /> </session-factory> </hibernate-configuration>
测试类:
/** * 一对一关联测试 */ public class Tests { Session session; Transaction tx; @Before public void initDate(){ session = HibernateUtil.getSession(); tx= session.beginTransaction(); } @After public void afterTest(){ tx.commit(); HibernateUtil.closeSession(); } /** * 一对一关联测试 */ @Test public void getTest(){ Users1 u1=new Users1(); u1.setUsername("火"); u1.setUserpass("2"); Resume1 r1=new Resume1(); r1.setResname("培训2"); r1.setRescardno("002"); u1.setResume1(r1); r1.setUsers1(u1); session.save(r1); System.out.println("ok-------"); } /** * 查询 */ @Test public void selectTest(){ Users1 u1=(Users1)session.load(Users1.class, 2); System.out.println(u1.getResume1().getResname()); }
结果:
数据库:
数据库:
Users1表:
Resume1表:
二:按照主键映射
Users2表的userid字段是主键,同时作为外键参照Resume2表的主键,即Users2表与Resume2表共享主键(Users2中的主键值是根据Resume2生成的主键值取值的)
Resume2:
package cn.entity_pk; /** * 档案类 * * @time * @author Happy * */ public class Resume2 { private Integer resid; private String resname; private String rescardno; private Users2 users2; public Users2 getUsers2() { return users2; } public void setUsers2(Users2 users2) { this.users2 = users2; } public Resume2() { } public Resume2( String resname, String rescardno) { this.resname = resname; this.rescardno = rescardno; } public Integer getResid() { return resid; } public void setResid(Integer resid) { this.resid = resid; } public String getResname() { return resname; } public void setResname(String resname) { this.resname = resname; } public String getRescardno() { return rescardno; } public void setRescardno(String rescardno) { this.rescardno = rescardno; } }
Users2:
package cn.entity_pk; /** * 员工类 * * @author Happy * */ public class Users2 { private Integer userid; private String username; private String userpass; private Resume2 resume2; public Resume2 getResume2() { return resume2; } public void setResume2(Resume2 resume2) { this.resume2 = resume2; } public Users2() { } public Users2(String username, String userpass) { this.username = username; this.userpass = userpass; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } }
Resume2.hbm.xml映射文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity_pk"> <class name="Resume2" table="RESUME2"> <id column="RESID" name="resid"> <generator class="sequence"> <param name="sequence">SEQ_NUM</param> </generator> </id> <property column="RESNAME" name="resname" type="string"/> <property column="RESCARDNO" name="rescardno" type="string"/> <!--主的一方 --> <one-to-one name="users2" cascade="all" class="Users2" /> </class> </hibernate-mapping>
Users2.hbm.xml映射文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity_pk"> <class name="Users2" table="USERS2"> <id name="userid" column="USERID" > <generator class="foreign"> <param name="property">resume2</param> </generator> </id> <property name="username" column="USERNAME" type="string"></property> <property name="userpass" column="USERPASS" type="string"></property> <!-- constrained:用来约束 在底层USERS2数据表中,植入外键--> <one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one> </class> </hibernate-mapping>
测试类:
public class Test_pk { Session session; Transaction tx; @Before public void initDate(){ session = HibernateUtil.getSession(); tx= session.beginTransaction(); } @After public void afterTest(){ tx.commit(); HibernateUtil.closeSession(); } /** * 一对一关联测试:按照主键映射 */ @Test public void getTest(){ Users2 u1=new Users2(); u1.setUsername("呵呵"); u1.setUserpass("1"); Resume2 r1=new Resume2(); r1.setResname("哈哈"); r1.setRescardno("001"); u1.setResume2(r1); r1.setUsers2(u1); session.save(r1); System.out.println("ok-------"); } /** * 查询 */ @Test public void selectTest(){ Users2 u1=(Users2)session.load(Users2.class, 2); System.out.println(u1.getResume2().getResname()); }
结果:
数据库:
Resume2表:
Users2表:
Hibernate一对一关系映射的更多相关文章
- hibernate(五) hibernate一对一关系映射详解
序言 之前讲解了一对多(单向.双向).多对多(双向),今天就讲解一下最后一个关系,一对一. 心情不错.状态也挺好的,赶紧写一篇博文造福一下大家把. --WH 一.一对一关系的概述 一对一关系看起来简单 ...
- Hibernate学习(五)———— hibernate一对一关系映射详解
一.一对一关系的概述 一对一关系看起来简单,其实也挺复杂的.其中关系就包含了四种,单向双向和主键关联外键关联. 什么意思呢,也就是包含了单向一对一主键关联.双向一对一主键关联,单向一对一外键关联,双向 ...
- Hibernate One-to-One Mappings 一对一关系映射
Hibernate One-to-One Mappings 一对一关系映射 关键:一对一关系映射和多对一关系映射非常像.仅仅是unique 属性值为 true 样例:一个员工仅仅能有一个地址. Hib ...
- hibernate(3) —— 关系映射
hibernate中关系映射指的是实体类与实体类间的关系.和数据库中表与表之间的关系类似,有一对一,多对一,一对多,多对多四种映射关系. 一:一对一映射 两个对象之间是一对一的关系,如人和身份证之间是 ...
- Hibernate注解关系映射
Hibernate Annotation关系映射的几种类型映射用法及使用方法(说明:以前实例的实体是user和role,主键分别是userid和roleid) 1)一对一外键关联映射(单向) @O ...
- 问题记录:EntityFramework 一对一关系映射
EntityFramework 一对一关系映射有很多种,比如主键作为关联,配置比较简单,示例代码: public class Teacher { public int Id { get; set; } ...
- mybatis中一对一关系映射
一对一关系中普通的配置方式 一.多表连接查询语句: <select id="selectStudentWithAddress" parameterType="int ...
- hibernate对象关系映射( 一对一,一对多,多对一,多对多的单向,双向映射 ——)
对象之间的关系: 关系映射之间的关系只的是对象之间的关系,并不指数据库表的关系(外键关系)这儿解决的问题是当对象之间的关系之一时,数据库表该如何映射,编程上如何对待. 一对一(主键关联,和单向的外键关 ...
- Hibernate 、多表关联映射 - 一对一关系映射(one- to-one)
hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...
随机推荐
- SharePoint Server 2016 IT Preview 先睹为快
1.首先下载SharePoint Server 2016 IT Preview安装包,大约2.8GB,当然如果有需要,还可以下载两种提供测试的语言包,其他语言包还未提供下载,如下图: 2.我是在已经安 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q9-Q12)
Question 9 You are designing an external binary large object (BLOB) store provider by implementing t ...
- Android Contextual Menus之一:floating context menu
Android Contextual Menus之一:floating context menu 上下文菜单 上下文相关的菜单(contextual menu)用来提供影响UI中特定item或者con ...
- Gradle基础
什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. Gr ...
- 【代码笔记】iOS-缓存路径操作类
一,代码. AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplica ...
- 同步时间linux
针对对时间要求精确度高的服务器 1.安装时间服务器yum install ntp 2.同步时间ntpdate time.nist.gov 3.设置计划任务每隔10分钟同步一次 */10 * * * * ...
- Git详解之三 Git分支
相关文档 — 更多 Git 基础培训.ppt GIT 使用经验.ppt GIT 介绍.pptx GIT 分支管理是一门艺术.docx Eclipse上GIT插件EGIT使用手册.docx git/gi ...
- Lojic X
媒体 赫兹 电话 500HZ 网络数据 8000HZ CD 44100HZ 电脑 48000HZ DVD 96000HZ 最大值(蓝光) 192000HZ 横向———————— 清晰度 ...
- mysql5.6主从参数详解
mysql5.6的主从相当的不错,增加了不少参数,提升了主从同步的安全和效率,以下是mysql5.6主从参数详解. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- Keepalived+MySQL双主架构
l 架构准备 Node1 192.168.15.3 Node2 192.168.15.4 VIP 192.168.15.254 l 软件 MySQL 5.6 Keepalive yum insta ...