标签:
一、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.entity.Users2" table="USERS2" schema="HOLLY" > 5 <!-- 使用了foreign主键生成策略,hibernate就会保证users2对象与关联的resume2对象共享同一个OID(对象标识符) --> 6 <id name="userid" type="java.lang.Integer" column="USERID"> 7 <!-- userid是users2的外键,是resume2的主键 --> 8 <generator class="foreign"> 9 <param name="property">resume2</param> 10 </generator> 11 </id> 12 <property name="username" type="java.lang.String" column="USERNAME"/> 13 <property name="userpass" type="java.lang.String" column="USERPASS"/> 14 15 16 <!-- constrainned为true,表示users2表的userid主键同时作为外键参照resume2的主键 --> 17 <one-to-one name="resume2" class="com.entity.Resume2" constrained="true"/> 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.entity.Resume2" table="RESUME2" schema="holly"> 5 <!-- 主键配置 --> 6 <id name="resid" type="java.lang.Integer" column="RESID"> 7 <!-- 主键由应用程序负责生成 --> 8 <generator class="assigned"/> 9 </id> 10 <property name="resname" type="java.lang.String" column="RESNAME"/> 11 12 <property name="rescardno" type="java.lang.String" column="RESCARDNO"/> 13 <!-- 一个个档案对应一个人 --> 14 <one-to-one name="users2" class="com.entity.Users2" cascade="all"/> 15 </class> 16 </hibernate-mapping>
5.在com.dao包下创建Resume2Dao.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.Users2; 15 import com.entity.Resume2; 16 import com.util.HibernateUtil; 17 18 public class Resume2Dao { 19 20 public static void main(String[] args) { 21 // saveResume2(); 22 loadResume2(); 23 24 } 25 /** 26 * 2.立即加载:当查询档案信息时查询账号信息 27 */ 28 private static void loadResume2() { 29 Session session=HibernateUtil.getSession(); 30 Resume2 resume2=(Resume2) session.load(Resume2.class, 3); 31 System.out.println("姓名:"+resume2.getResname()); 32 System.out.println("用户名:"+resume2.getUsers2().getUsername()); 33 34 HibernateUtil.closeSession(); 35 36 } 37 /** 38 * 1.一对一 按照主键键映射:级联操作,保存档案信息时,保存员工账号信息 39 * 2条插入sql,查询sql见readme.txt 40 * 41 */ 42 private static void saveResume2() { 43 Session session=HibernateUtil.getSession(); 44 Transaction tx=session.beginTransaction(); 45 //创建员工档案对象 46 Resume2 resume2=new Resume2(3, "aaaa", "2222210"); 47 48 //☆☆☆☆创建员工账户信息☆☆☆☆ 49 Users2 users2=new Users2("holly", "123"); 50 51 //给员工档案的users1对象赋值 52 resume2.setUsers2(users2); 53 54 //给员工账号的resume1对象赋值 55 users2.setResume2(resume2); 56 57 //添加员工档案信息时级联保存员工账号信息 58 session.save(resume2); 59 tx.commit(); 60 System.out.println("添加成功"); 61 62 HibernateUtil.closeSession(); 63 64 } 65 66 }
6.运行结果如下
1 /** 2 * 1.一对一 按照主键映射:级联操作,保存档案信息时,保存员工账号信息 3 * 2条插入sql,查询sql见readme.txt 4 * 5 */ 6 Hibernate: 7 insert 8 into 9 holly.RESUME2 10 (RESNAME, RESCARDNO, RESID) 11 values 12 (?, ?, ?) 13 Hibernate: 14 insert 15 into 16 HOLLY.USERS2 17 (USERNAME, USERPASS, USERID) 18 values 19 (?, ?, ?) 20 21 /** 22 * 2.延迟加载:当查询档案信息时查询账号信息 23 */ 24 25 Hibernate: 26 select 27 resume2x0_.RESID as RESID0_1_, 28 resume2x0_.RESNAME as RESNAME0_1_, 29 resume2x0_.RESCARDNO as RESCARDNO0_1_, 30 users2x1_.USERID as USERID1_0_, 31 users2x1_.USERNAME as USERNAME1_0_, 32 users2x1_.USERPASS as USERPASS1_0_ 33 from 34 holly.RESUME2 resume2x0_ 35 left outer join 36 HOLLY.USERS2 users2x1_ 37 on resume2x0_.RESID=users2x1_.USERID 38 where 39 resume2x0_.RESID=? 40 姓名:aaaa 41 用户名:holly
标签:
原文地址:http://www.cnblogs.com/holly8/p/5774456.html