一对一关联
Hibernate中的一对一关联
Hibernate提供了两种一对一映射关联关系的方式:
1)按照外键映射
2)按照主键映射
下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映射方式分别完成以下持久化操作
(1)保存员工档案的同时分配给员工一个账号
(2)加载员工档案的同时加载账号信息
一丶按照外键映射
Users1中:
1 package cn.xsy.entity.day_onetoone; 2 3 /** 4 * Created by Administrator on 2017/12/30. 5 */ 6 public class Users1 { 7 8 private Integer userid; 9 10 private String username; 11 12 private String userpass; 13 14 private Resume1 resume1; 15 16 public Integer getUserid() { 17 return userid; 18 } 19 20 public void setUserid(Integer userid) { 21 this.userid = userid; 22 } 23 24 public String getUsername() { 25 return username; 26 } 27 28 public void setUsername(String username) { 29 this.username = username; 30 } 31 32 public String getUserpass() { 33 return userpass; 34 } 35 36 public void setUserpass(String userpass) { 37 this.userpass = userpass; 38 } 39 40 public Resume1 getResume1() { 41 return resume1; 42 } 43 44 public void setResume1(Resume1 resume1) { 45 this.resume1 = resume1; 46 } 47 }
Users1.hbm.xml中:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <!--根节点--> 6 <!-- package:要映射的对象所在的包(可选,如果不指定,此文件下所有的类都要指定全路径) 7 auto-import 默认为true,在写HQL的时候自动导入包名 8 如果指定为false,在写HQL的时候必须要写上类的全名--> 9 <hibernate-mapping package="cn.xsy.entity.day_onetoone"> 10 <class name="Users1" table="USERS1"> 11 <id name="userid" column="USERID" > 12 <generator class="native"></generator> 13 </id> 14 <property name="username" column="USERNAME" type="string"></property> 15 <property name="userpass" column="USERPASS" type="string"></property> 16 <one-to-one name="resume1" class="Resume1" property-ref="users1"></one-to-one> 17 </class> 18 </hibernate-mapping>
Resume1中:
1 package cn.xsy.entity.day_onetoone; 2 3 /** 4 * Created by Administrator on 2017/12/30. 5 */ 6 public class Resume1 { 7 8 private Integer resid; 9 10 private String resname; 11 12 private String rescardno; 13 14 private Users1 users1; 15 16 public Integer getResid() { 17 return resid; 18 } 19 20 public void setResid(Integer resid) { 21 this.resid = resid; 22 } 23 24 public String getResname() { 25 return resname; 26 } 27 28 public void setResname(String resname) { 29 this.resname = resname; 30 } 31 32 public String getRescardno() { 33 return rescardno; 34 } 35 36 public void setRescardno(String rescardno) { 37 this.rescardno = rescardno; 38 } 39 40 public Users1 getUsers1() { 41 return users1; 42 } 43 44 public void setUsers1(Users1 users1) { 45 this.users1 = users1; 46 } 47 }
Resume1.hbm.xml中:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <!--根节点--> 6 <!-- package:要映射的对象所在的包(可选,如果不指定,此文件下所有的类都要指定全路径) 7 auto-import 默认为true,在写HQL的时候自动导入包名 8 如果指定为false,在写HQL的时候必须要写上类的全名--> 9 <hibernate-mapping package="cn.xsy.entity.day_onetoone"> 10 <class name="Resume1" table="RESUME1"> 11 <id name="resid" column="RESID" > 12 <generator class="native"></generator> 13 </id> 14 <property name="resname" column="RESNAME" type="string"></property> 15 <property name="rescardno" column="RESCARDNO" type="string"></property> 16 <many-to-one name="users1" class="Users1" cascade="all" column="RESUSERID" unique="true"></many-to-one> 17 </class> 18 </hibernate-mapping>
hibernate.cfgonetoone.xml中:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <!--创建Session的基础配置--> 8 <session-factory> 9 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 10 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 11 <property name="connection.username">waxsy</property> 12 <property name="connection.password">bsfm</property> 13 14 <!-- JDBC 连接池(use the built-in) --> 15 <!-- <property name="connection.pool_size">1</property>--> 16 17 <!-- SQL dialect (方言)--> 18 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 19 20 <!-- Enable Hibernate's automatic session context management 21 session工厂创建的session和当前线程绑定bind 22 --> 23 <property name="current_session_context_class">thread</property> 24 25 <!-- Disable the second-level cache 启用/关闭二级缓存--> 26 <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>--> 27 28 <!-- Echo all executed SQL to stdout 在控制台打印sql --> 29 <property name="show_sql">true</property> 30 <!--格式化SQL--> 31 <property name="format_sql">true</property> 32 <!-- Drop and re-create (重新创建)the database schema(架构) on startup (启动) 33 是否根据hbm.xml自动建表 建表的策略 update create 34 hbm配置文件》》》》》》》ddl 数据表 35 create: 过程:先drop 再Create 36 update:检查现有表的架构是否存在,真实开发中,在上线后,一定update 37 --> 38 <property name="hbm2ddl.auto">update</property> 39 <!--关联小配置 --> 40 41 42 <mapping resource="cn/xsy/entity/day_onetoone/Users1.hbm.xml"/> 43 <mapping resource="cn/xsy/entity/day_onetoone/Resume.hbm.xml"/> 44 </session-factory> 45 46 </hibernate-configuration 47 >
Test20171230_onetoone中:
1 package day01; 2 3 4 import cn.xsy.entity.day_manytomanydouble.Employee; 5 import cn.xsy.entity.day_manytomanydouble.Project; 6 import cn.xsy.entity.day_onetomanydouble.Dept; 7 import cn.xsy.entity.day_onetomanydouble.Emp; 8 import cn.xsy.entity.day_onetoone.Resume1; 9 import cn.xsy.entity.day_onetoone.Users1; 10 import cn.xsy.util.HibernateUtil; 11 import org.hibernate.Query; 12 import org.hibernate.Session; 13 import org.hibernate.Transaction; 14 import org.junit.Test; 15 16 import java.util.List; 17 import java.util.Set; 18 19 /** 20 * Created by Happy on 2017-12-24. 21 */ 22 public class Test20171230_onetoone { 23 24 //一对一关联 25 @Test 26 public void onetoone(){ 27 28 Session session = HibernateUtil.getSession(); 29 Transaction tx=session.beginTransaction(); 30 //创建一个用户对象 31 Users1 u1=new Users1(); 32 u1.setUserid(1); 33 u1.setUsername("小涩瑶"); 34 //创建一个档案对象 35 Resume1 r1=new Resume1(); 36 r1.setResid(1); 37 r1.setResname("余浪北笙"); 38 u1.setResume1(r1); 39 r1.setUsers1(u1); 40 //保存r1自动保存u1 41 session.save(r1); 42 tx.commit(); 43 System.out.println("add ok!"); 44 45 } 46 47 48 }
二丶按照主键映射