一、hibernate的一对一按照外键映射
1.创建如下sql脚本:
1 --员工账号表 2 create table USERS1 3 ( 4 USERID NUMBER(6) not null, 5 USERNAME VARCHAR2(20), 6 USERPASS VARCHAR2(20) 7 ) 8 ; 9 10 11 12 --档案表 13 create table RESUME1 14 ( 15 RESID NUMBER(6) not null, 16 RESUSERID NUMBER(6), 17 RESNAME VARCHAR2(20), 18 RESCARDNO VARCHAR2(20) 19 ) 20 ; 21 22 alter table USERS1 23 add constraint PK_USERS1_USERID primary key (USERID); 24 25 alter table RESUME1 26 add constraint PK_RESUME1_RESID primary key (RESID); 27 alter table RESUME1 28 add constraint FK_RESUME1_RESUSERID foreign key (RESUSERID) 29 references USERS1 (USERID);
2.创建如下web项目结构
3.在项目的src根目录下创建主配置文件hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 <property name="connection.username">holly</property> 8 <property name="connection.password">sys</property> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="show_sql">true</property> 11 <property name="format_sql">true</property> 12 13 <mapping resource="com/entity/Resume1.hbm.xml"/> 14 <mapping resource="com/entity/Users1.hbm.xml"/> 15 </session-factory> 16 </hibernate-configuration>
4.在项目的src的com.entity包下创建Users1.java类
1 package com.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 /** 6 * 员工账号表 7 * @author Dell 8 * 9 */ 10 public class Users1 { 11 private Integer userid; //员工账号id 12 private String username; //员工姓名 13 private String userpass; //员工账号密码 14 private Resume1 resume1; //档案对象 15 16 public Users1() { 17 } 18 public Users1(Integer userid, String username, String userpass) { 19 this.userid = userid; 20 this.username = username; 21 this.userpass = userpass; 22 } 23 public Users1(Integer userid, String username, String userpass, 24 Resume1 resume1) { 25 this.userid = userid; 26 this.username = username; 27 this.userpass = userpass; 28 this.resume1 = resume1; 29 } 30 public Integer getUserid() { 31 return userid; 32 } 33 public void setUserid(Integer userid) { 34 this.userid = userid; 35 } 36 public String getUsername() { 37 return username; 38 } 39 public void setUsername(String username) { 40 this.username = username; 41 } 42 public String getUserpass() { 43 return userpass; 44 } 45 public void setUserpass(String userpass) { 46 this.userpass = userpass; 47 } 48 49 public Resume1 getResume1() { 50 return resume1; 51 } 52 public void setResume1(Resume1 resume1) { 53 this.resume1 = resume1; 54 } 55 @Override 56 public String toString() { 57 return "Users1 [resume1=" + resume1 + ", userid=" + userid 58 + ", username=" + username + ", userpass=" + userpass + "]"; 59 } 60 61 62 63 }
5.在项目的src的com.entity包下创建Users1.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Users1" table="USERS1" schema="HOLLY" > 5 <id name="userid" type="java.lang.Integer" column="USERID"> 6 <generator class="assigned"/> 7 </id> 8 <property name="username" type="java.lang.String" column="USERNAME"/> 9 <property name="userpass" type="java.lang.String" column="USERPASS"/> 10 11 12 <!-- 多对一映射:name是emp中外键字段引用的对象,class是对象的类型,column是数据库中对应的外键列字段名 --> 13 <one-to-one name="resume1" class="com.entity.Resume1" property-ref="users1"/> 14 </class> 15 </hibernate-mapping>
6.在项目的src的com.entity包下创建Resume1.java类
1 package com.entity; 2 3 import java.util.Date; 4 /** 5 * 档案表 6 * @author Dell 7 * 8 */ 9 public class Resume1 { 10 private Integer resid; //档案编号 11 private Users1 users1; //用户对象 12 private String resname; //档案名称 13 private String rescardno; //档案卡号 14 15 public Resume1() { 16 } 17 18 19 public Resume1(Integer resid, String resname, String rescardno) { 20 this.resid = resid; 21 this.resname = resname; 22 this.rescardno = rescardno; 23 } 24 25 26 public Resume1(Integer resid, Users1 users1, String resname, 27 String rescardno) { 28 this.resid = resid; 29 this.users1 = users1; 30 this.resname = resname; 31 this.rescardno = rescardno; 32 } 33 34 public Integer getResid() { 35 return resid; 36 } 37 38 public void setResid(Integer resid) { 39 this.resid = resid; 40 } 41 42 public Users1 getUsers1() { 43 return users1; 44 } 45 46 public void setUsers1(Users1 users1) { 47 this.users1 = users1; 48 } 49 50 public String getResname() { 51 return resname; 52 } 53 54 public void setResname(String resname) { 55 this.resname = resname; 56 } 57 58 public String getRescardno() { 59 return rescardno; 60 } 61 62 public void setRescardno(String rescardno) { 63 this.rescardno = rescardno; 64 } 65 66 @Override 67 public String toString() { 68 return "Resume1 [rescardno=" + rescardno + ", resid=" + resid 69 + ", resname=" + resname + ", users1=" + users1 + "]"; 70 } 71 72 73 74 75 76 }
7.在项目的src的com.entity包下创建Resume1.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Resume1" table="RESUME1" schema="holly"> 5 <!-- 主键配置 --> 6 <id name="resid" type="java.lang.Integer" column="RESID"> 7 <!-- 主键由应用程序负责生成 --> 8 <generator class="assigned"/> 9 </id> 10 <!-- 部门名称 --> 11 <property name="resname" type="java.lang.String" column="RESNAME"/> 12 13 <!-- 部门位置 --> 14 <property name="rescardno" type="java.lang.String" column="RESCARDNO"/> 15 <!-- 一个个档案对应一个人 --> 16 <!-- unique表示唯一约束,确保用户唯一 unique=true表示一一对应 --> 17 <many-to-one name="users1" class="com.entity.Users1" column="RESUSERID" cascade="all" unique="true"/> 18 </class> 19 </hibernate-mapping>
8.在项目的src的ccom.util包下创建HibernateUtil.java
1 package com.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 /** 10 * 创建线程池来管理Session 11 */ 12 private static ThreadLocal<Session> thread=new ThreadLocal<Session>(); 13 /** 14 * 创建读取配置文件的对象 15 */ 16 private static Configuration config=null; 17 /** 18 * 创建获取Session的工厂 19 */ 20 private static SessionFactory factory=null; 21 22 /** 23 * 读取配置文件 24 */ 25 static{ 26 try { 27 config=new Configuration().configure("/hibernate.cfg.xml"); 28 factory=config.buildSessionFactory(); 29 } catch (HibernateException e) { 30 System.out.println("读取配置文件或创建SessionFactory失败!"); 31 e.printStackTrace(); 32 } 33 } 34 35 /** 36 * 获取session 37 * @return 38 */ 39 public static Session getSession(){ 40 Session session=thread.get(); 41 if(session==null){ 42 session=factory.openSession(); 43 thread.set(session); 44 } 45 return session; 46 } 47 48 /** 49 * 关闭Session 50 */ 51 public static void closeSession(){ 52 Session session=thread.get(); 53 thread.set(null); 54 session.close(); 55 } 56 57 58 }
9.在项目的src的com.dao包下创建Resume1Dao.java
1 package com.dao; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.List; 7 import java.util.Set; 8 import java.util.logging.SimpleFormatter; 9 10 import org.hibernate.Query; 11 import org.hibernate.Session; 12 import org.hibernate.Transaction; 13 14 import com.entity.Users1; 15 import com.entity.Resume1; 16 import com.util.HibernateUtil; 17 18 public class Resume1Dao { 19 20 public static void main(String[] args) { 21 //saveResume1(); 22 loadResume1(); 23 24 } 25 /** 26 * 2.立即加载:当查询档案信息时查询账号信息 27 * 28 */ 29 private static void loadResume1() { 30 Session session=HibernateUtil.getSession(); 31 Resume1 resume1=(Resume1) session.get(Resume1.class, 1); 32 System.out.println("姓名:"+resume1.getResname()); 33 System.out.println("用户名:"+resume1.getUsers1().getUsername()); 34 35 HibernateUtil.closeSession(); 36 37 } 38 /** 39 * 1.一对一 按照外键映射:级联操作,保存档案信息时,保存员工账号信息 40 * 1条查询sql,2条插入sql,查询sql见readme.txt 41 */ 42 private static void saveResume1() { 43 Session session=HibernateUtil.getSession(); 44 Transaction tx=session.beginTransaction(); 45 //创建员工档案对象 46 Resume1 resume1=new Resume1(1, "李四", "2014010101010"); 47 48 //创建员工账户信息 49 Users1 users1=new Users1(1, "lisi", "123"); 50 51 //给员工档案的users1对象赋值 52 resume1.setUsers1(users1); 53 54 //给员工账号的resume1对象赋值 55 users1.setResume1(resume1); 56 57 //添加员工档案信息时级联保存员工账号信息 58 session.save(resume1); 59 tx.commit(); 60 System.out.println("添加成功"); 61 62 HibernateUtil.closeSession(); 63 64 } 65 66 }
10.运行结果如下:
1 /** 2 * 1.一对一 按照外键映射:级联操作,保存档案信息时,保存员工账号信息 3 * 1条查询sql,2条插入sql,查询sql见readme.txt 4 * 5 */ 6 Hibernate: 7 select 8 users1x_.USERID, 9 users1x_.USERNAME as USERNAME1_, 10 users1x_.USERPASS as USERPASS1_ 11 from 12 HOLLY.USERS1 users1x_ 13 where 14 users1x_.USERID=? 15 Hibernate: 16 insert 17 into 18 HOLLY.USERS1 19 (USERNAME, USERPASS, USERID) 20 values 21 (?, ?, ?) 22 Hibernate: 23 insert 24 into 25 holly.RESUME1 26 (RESNAME, RESCARDNO, RESUSERID, RESID) 27 values 28 (?, ?, ?, ?) 29 /** 30 * 2.立即加载:当查询档案信息时查询账号信息 31 */ 32 33 Hibernate: 34 select 35 resume1x0_.RESID as RESID0_0_, 36 resume1x0_.RESNAME as RESNAME0_0_, 37 resume1x0_.RESCARDNO as RESCARDNO0_0_, 38 resume1x0_.RESUSERID as RESUSERID0_0_ 39 from 40 holly.RESUME1 resume1x0_ 41 where 42 resume1x0_.RESID=? 43 姓名:李四 44 Hibernate: 45 select 46 users1x0_.USERID as USERID1_1_, 47 users1x0_.USERNAME as USERNAME1_1_, 48 users1x0_.USERPASS as USERPASS1_1_, 49 resume1x1_.RESID as RESID0_0_, 50 resume1x1_.RESNAME as RESNAME0_0_, 51 resume1x1_.RESCARDNO as RESCARDNO0_0_, 52 resume1x1_.RESUSERID as RESUSERID0_0_ 53 from 54 HOLLY.USERS1 users1x0_ 55 left outer join 56 holly.RESUME1 resume1x1_ 57 on users1x0_.USERID=resume1x1_.RESUSERID 58 where 59 users1x0_.USERID=? 60 Hibernate: 61 select 62 resume1x0_.RESID as RESID0_0_, 63 resume1x0_.RESNAME as RESNAME0_0_, 64 resume1x0_.RESCARDNO as RESCARDNO0_0_, 65 resume1x0_.RESUSERID as RESUSERID0_0_ 66 from 67 holly.RESUME1 resume1x0_ 68 where 69 resume1x0_.RESUSERID=? 70 用户名:lisi 71
二、Hibernate一对一按照主键查询
1.创建如下sql语句
1 create table RESUME2 2 ( 3 RESID NUMBER(6) not null, 4 RESNAME VARCHAR2(20), 5 RESCARDNO VARCHAR2(20) 6 ) 7 ; 8 9 10 create table USERS2 11 ( 12 USERID NUMBER(6) not null, 13 USERNAME VARCHAR2(20) not null, 14 USERPASS VARCHAR2(20) not null 15 ) 16 ; 17 18 alter table RESUME2 19 add constraint PK_RESUME2_RESID primary key (RESID); 20 21 22 alter table USERS2 23 add constraint PK_USERS2_USERID primary key (USERID) 24 disable; 25 alter table USERS2 26 add constraint FK_USERS2_USERID foreign key (USERID) 27 references RESUME2 (RESID);
2.项目的主配置,工具类,实体类都和上面一样,此处省略
3.在com.entity包下编辑Users2.hbm.xml映射文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.pojo.Users2" table="USERS2"> 5 <id name="usersid" type="java.lang.Integer" column="USERSID"> 6 <!-- 双向一对一:按照外键关联映射配置 --> 7 <generator class="foreign"> 8 <!-- 当前类User2外键列所关联的另一方Resume2类的对象属性名resume2 --> 9 <param name="property">resume2</param> 10 </generator> 11 </id> 12 <property name="usersname" type="java.lang.String" column="USERSNAME"/> 13 <property name="userspwd" type="java.lang.String" column="USERSPWD"/> 14 15 <!-- 当前users2表中的usersid列又作为主键,又作为外键 --> 16 <one-to-one name="resume2" class="com.pojo.Resume2" 17 constrained="true" cascade="all"/> 18 </class> 19 </hibernate-mapping>
4.在com.entity包下编辑Resume2.hbm.xml映射文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.pojo.Resume2" table="RESUME2"> 5 <id name="resid" type="java.lang.Integer" column="RESID"> 6 <generator class="assigned"/> 7 </id> 8 <property name="resname" type="java.lang.String" column="RESNAME"/> 9 <property name="rescard" type="java.lang.String" column="RESCARD"/> 10 11 <one-to-one name="users2" class="com.pojo.Users2"/> 12 13 </class> 14 </hibernate-mapping>
5.在com.dao包下创建Test.java
1 package com.test; 2 3 import org.hibernate.Session; 4 5 import com.pojo.Resume2; 6 import com.pojo.Users2; 7 import com.util.HibernateUtil; 8 9 10 public class Test { 11 public static void main(String[] args) { 12 //1.级联添加操作 13 //saveResumeToUsers(); 14 //2.根据主键查询 15 loadResume(); 16 17 } 18 /** 19 * 1.级联添加操作 20 Hibernate: select resume2x_.RESID, resume2x_.RESNAME as RESNAME0_, resume2x_.RESCARD as RESCARD0_ from RESUME2 resume2x_ where resume2x_.RESID=? 21 Hibernate: insert into RESUME2 (RESNAME, RESCARD, RESID) values (?, ?, ?) 22 Hibernate: insert into USERS2 (USERSNAME, USERSPWD, USERSID) values (?, ?, ?) 23 */ 24 private static void saveResumeToUsers() { 25 //1.获取session对象 26 Session session=HibernateUtil.getSession(); 27 28 //2.创建瞬时对象 29 Users2 users1=new Users2(3, "倩倩", "123"); 30 Resume2 resume1=new Resume2(3, "倩倩档案", "qianqian320112"); 31 32 users1.setResume2(resume1); 33 resume1.setUsers2(users1); 34 35 //3.级联添加 36 session.save(users1); 37 38 session.beginTransaction().commit(); 39 //4.提交事务 40 System.out.println("save cascade success..."); 41 } 42 /** 43 Hibernate: select resume2x0_.RESID as RESID0_1_, resume2x0_.RESNAME as RESNAME0_1_, resume2x0_.RESCARD as RESCARD0_1_, users2x1_.USERSID as USERSID1_0_, users2x1_.USERSNAME as USERSNAME1_0_, users2x1_.USERSPWD as USERSPWD1_0_ from RESUME2 resume2x0_ left outer join USERS2 users2x1_ on resume2x0_.RESID=users2x1_.USERSID where resume2x0_.RESID=? 44 Resume2 [rescard=qianqian320112, resid=3, resname=倩倩档案] 45 Users2 [usersid=3, usersname=倩倩, userspwd=123] 46 */ 47 private static void loadResume() { 48 //1.获取session对象 49 Session session=HibernateUtil.getSession(); 50 51 //2.根据主键去查询 52 //不会出现sql,xml默认就是延时记载 53 Resume2 resume1=(Resume2) session.load(Resume2.class, 3); 54 System.out.println(resume1); 55 System.out.println(resume1.getUsers2()); 56 57 58 } 59 60 }
6.运行结果如下