Hibernate提供了兩種映射一對一映射關聯關係的方式: 01.按照外鍵映射 02.按照主鍵映射 下麵以員工賬號表和員工檔案表(員工賬號和檔案表之間是一對一的關係)為例,介紹這兩種映射關係,並使用這兩種 映射方式分別完成以下持久化操作 (1)保存員工檔案的同時分配給員工一個賬號 (2)載入員工檔案 ...
Hibernate提供了兩種映射一對一映射關聯關係的方式:
01.按照外鍵映射
02.按照主鍵映射
下麵以員工賬號表和員工檔案表(員工賬號和檔案表之間是一對一的關係)為例,介紹這兩種映射關係,並使用這兩種 映射方式分別完成以下持久化操作
(1)保存員工檔案的同時分配給員工一個賬號
(2)載入員工檔案的同時載入賬號信息
一:按照外鍵映射
需要提示:
HibernateUtil工具類(用於獲取session和關閉session)
package cn.zhang.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(); } }
Resume1.java實體類
package cn.zhang.entity; public class Resume1 { private Integer resid; private String resname; private String rescardno; private Users1 users1; public Resume1(String resname, String rescardno) { super(); this.resname = resname; this.rescardno = rescardno; } public Resume1() { } 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.java實體類
package cn.zhang.entity; public class Users1 { private Integer userid; private String username; private String userpass; private Resume1 resume1; public Users1(String username, String userpass) { super(); this.username = username; this.userpass = userpass; } public Users1() { } 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; } }
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.zhang.entity"> <class name="Users1" table="USERS1"> <id name="userid" column="USERID" > <generator class="sequence"> <param name="sequence">SQ_Num</param> </generator> </id> <property name="username" column="USERNAME" type="string"></property> <property name="userpass" column="USERPASS" type="string"></property> <!-- 一對一關聯 --> <!-- property-ref屬性為users1,表明通過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.zhang.entity"> <class name="Resume1" table="RESUME1"> <id name="resid" column="RESID" > <generator class="sequence"> <param name="sequence">SQ_Num</param> </generator> </id> <property name="resname" column="RESNAME" type="string"></property> <property name="rescardno" column="RESCARDNO" type="string"></property> <!-- 一對一配置 --> <!--column="RESUSERID": Resume1對應Users1表的外鍵關係 --> <!--unique="true":表明每個Resume1對象都有唯一的Users1對象,確保唯一性--> <!--cascade="all":級聯 --> <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>
測試類:
/** * 一對一關聯測試 */ @Test public void testOne(){ Session session = HibernateUtil.currentSession(); Transaction tx=session.beginTransaction(); //創建一個用戶對象 Users1 u1=new Users1("ZhangZong","521"); //創建一個檔案對象 Resume1 r1=new Resume1("精英檔案","好"); u1.setResume1(r1); r1.setUsers1(u1); //保存r1自動保存u1 session.save(r1);//以為Resume1設置了級聯,保存r1,u1也會保存 tx.commit(); HibernateUtil.closeSession(); System.out.println("成功"); }
結果展示:
01.控制台
02.資料庫
users1表
resume1表
二:按照主鍵映射
Users2表的userid欄位是主鍵,同時作為外鍵參照Resume2表的主鍵,即Users2表與Resume2表共用主鍵(Users2中的主鍵值是根據Resume2生成的主鍵值取值的)
需要提示:
Resume2實體類:
package cn.zhang.entity; public class Resume2 { private Integer resid; private String resname; private String rescardno; private Users2 users2; public Resume2(String resname, String rescardno) { super(); this.resname = resname; this.rescardno = rescardno; } public Resume2() { } 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 Users2 getUsers2() { return users2; } public void setUsers2(Users2 users2) { this.users2 = users2; } }
Users2.java實體類
package cn.zhang.entity; public class Users2 { private Integer userid; private String username; private String userpass; private Resume2 resume2; public Users2(String username, String userpass) { super(); this.username = username; this.userpass = userpass; } public Users2() { } 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 Resume2 getResume2() { return resume2; } public void setResume2(Resume2 resume2) { this.resume2 = resume2; } }
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.zhang.entity"> <class name="Resume2" table="RESUME2"> <!-- 主鍵的生成策略,Users2表中的主鍵userid會根據此來取值 --> <id name="resid" column="RESID" > <generator class="sequence"> <param name="sequence">SQ_Num</param> </generator> </id> <property name="resname" column="RESNAME" type="string"></property> <property name="rescardno" column="RESCARDNO" type="string"></property> <!-- 一對一配置 --> <one-to-one name="users2" class="Users2" cascade="all"></one-to-one> </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.zhang.entity"> <class name="Users2" table="USERS2"> <!-- foreign生成策略,根據Resume2的主鍵生成策略 --> <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="true":表明 Users2表的userid主鍵同時作為外鍵參照Resume2表的主鍵--> <one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one> </class> </hibernate-mapping>
測試:
/** * 一對一關聯測試:按照主鍵映射 */ @Test public void testOne(){ Session session = HibernateUtil.currentSession(); Transaction tx=session.beginTransaction(); //創建一個用戶對象 Users2 u1=new Users2("ZhangZong","521"); //創建一個檔案對象 Resume2 r1=new Resume2("精英檔案","好"); u1.setResume2(r1); r1.setUsers2(u1); //保存r1自動保存u1 session.save(r1); tx.commit(); HibernateUtil.closeSession(); System.out.println("成功"); }
結果展示:
01.控制台
02.資料庫
Resume2表
Users2表
三:組件的映射
建立關係數據模型的一個重要原則是在不會導致數據冗餘的前提下,儘可能減少資料庫表中的數目及表之間的外鍵參照關係。以員工信息為例,員工信息中有員工的家庭地址信息,如果把地址信息單獨放在一張表中,然後建立員工信息表和地址信息表之間的外鍵關係,當每次查詢員工信息時,都需要建立這兩個表的連接。建立表的連接是很耗時的操作,為了提高資料庫運行性能,可以把這兩張表的信息整合在一張員工信息表Empinfo中
需要提示:
EmpHomeAddress.java實體類
package cn.zhang.entity; //員工地址信息實體類 public class EmpHomeAddress { private String ehomestreet; private String ehomecity; private String ehomeprovince; private String ehomezipcode; private EmpInfo empinfo; public String getEhomestreet() { return ehomestreet; } public void setEhomestreet(String ehomestreet) { this.ehomestreet = ehomestreet; } public String getEhomecity() { return ehomecity; } public void setEhomecity(String ehomecity) { this.ehomecity = ehomecity; } public String getEhomeprovince() { return ehomeprovince; } public void setEhomeprovince(String ehomeprovince) { this.ehomeprovince = ehomeprovince; } public String getEhomezipcode() { return ehomezipcode; } public void setEhomezipcode(String ehomezipcode) { this.ehomezipcode = ehomezipcode; } public EmpInfo getEmpinfo() { return empinfo; } public void setEmpinfo(EmpInfo empinfo) { this.empinfo = empinfo; } }
EmpInfo.java實體類
package cn.zhang.entity; //員工實體類 public class EmpInfo { private Integer eid; private String ename; private EmpHomeAddress ehome;//地址信息 public Integer getEid() { return eid; } public void setEid(Integer eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public EmpHomeAddress getEhome() { return ehome; } public void setEhome(EmpHomeAddress ehome) { this.ehome = ehome; } }
EmpInfo.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.zhang.entity"> <class name="EmpInfo" table="EMPINFO"> <id name="eid" column="EID"> <generator class="native"></generator> </id> <property name="ename" column="ENAME" type="string"></property> <!--組件的映射配置 --> <!--component元素表明 ehome屬性是EmpInfo類的一個組成部分 name設定被映射的持久化類的屬性名 --> <component name="ehome" class="EmpHomeAddress"> <!-- parent指定EmpHomeAddress類所屬的整體類 --> <parent name="empinfo"/> <!-- 基本映射 --> <property name="ehomestreet" column="EHOMESTREET" type="string"></property> <property name="ehomecity" column="EHOMECITY" type="string"></property> <property name="ehomeprovince" column="EHOMEPROVINCE" type="string"></property> <property name="ehomezipcode" column="EHOMEZIPCODE" type="string"></property> </component> </class> </hibernate-mapping>
測試類:
/** * 組件的映射測試 */ @Test public void testOne(){ Session session = HibernateUtil.currentSession(); Transaction tx=session.beginTransaction(); //創建一個員工對象 EmpInfo emp=new EmpInfo(); emp.setEname("張總"); //創建一個員工地址對象 EmpHomeAddress address=new EmpHomeAddress(); address.setEhomecity("北京"); address.setEhomeprovince("北京"); address.setEhomestreet("五道口"); address.setEhomezipcode("100000"); address.setEmpinfo(emp); emp.setEhome(address); session.save(emp); tx.commit(); HibernateUtil.closeSession(); System.out.println("成功"); }
測試結果展示:
01.控制台
02.資料庫
EMPINFO表: