Java的Hibernate框架中的組合映射學習教程
一、組合映射
組合是關聯關系的一種特殊情況,是關聯關系耦合度最高的一種關系,組合的主對象和子對象擁有相同的生命周期,主對像消亡的話子對象也會消亡。這里使用雇主和用戶作為示例,用戶和雇主都擁有聯系方式屬性,如果這里站在對象角度思考的話,常常會把對象模型繪制成為組合的方式,抽象出來一個共同的聯系方式類,然后兩種人分別包含相應的聯系方式對象即可,向應的對象模型時它的對象示例如下圖所示:
組合對象模型在生成相應的關系模型后會把對應的子類包含到主表中,所以對應的表結構會將相應的屬性生成到對應的表中,相應的表結構如下:
1.1 Employee類及映射文件
在對象模型中Employee和Contact之間擁有包含關系,在編寫代碼時需要將Contact對象包含在Employee中。對應的映射文件中也需要有Contact對象的映射,需要使用<component>標簽來標明組合的對象,并把對象的屬性添加到對象標簽中。
清單一:Employee.java,類文件中除了基本的屬性外還需要分裝Contact對象,因為它們之間有一層包含關系。
package com.src.hibernate; public class Employee { //id號 private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } //名稱 private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //聯系對象 private Contact userContact; public Contact getUserContact() { return userContact; } public void setUserContact(Contact userContact) { this.userContact = userContact; } }
清單二:Employee.hbm.xml,添加對應的映射文件,映射的組合對象要使用<component>來標明,并在該標簽中添加對應的對象屬性,具體如下代碼:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.src.hibernate.Employee" table="t_employee"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <component name="employeeContact"> <property name="email"/> <property name="address"/> <property name="zipCode"/> <property name="contactTel"/> </component> </class> </hibernate-mapping>
1.2 User類及配置文件
清單三:User.java,它的內容結構和Employee.java的相同,其它的不再多說,看代碼:
package com.src.hibernate; public class User { //id號 private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } //姓名 private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //聯系對象 private Contact userContact; public Contact getUserContact() { return userContact; } public void setUserContact(Contact userContact) { this.userContact = userContact; } }
清單四:User.hbm.xml,它的內容結構同Employee.hbm.xml內容,主要是<component>標簽的使用,很簡單,代碼如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.src.hibernate.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <component name="userContact"> <property name="email"/> <property name="address"/> <property name="zipCode"/> <property name="contactTel"/> </component> </class> </hibernate-mapping>
1.3 Contact.java類
該類文件沒有什么需要注意的地方,添加基本的屬性即可,也不需要為該類配置對應的映射,所以它的內容相當的簡單。
package com.src.hibernate; public class Contact { //email地址 private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } //住址 private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } //郵編號 private String zipCode; public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } //聯系電話 private String contactTel; public String getContactTel() { return contactTel; } public void setContactTel(String contactTel) { this.contactTel = contactTel; } }
1.4 生成結果
經過上面的文件配置后接下來就可以生成相應的數據庫表結構了,生成的SQL語句如下:
drop table if exists t_employee drop table if exists t_user create table t_employee (id integer not null auto_increment, name varchar(255), email varchar(255), address varchar(255), zipCode varchar(255), contactTel varchar(255), primary key (id)) create table t_user (id integer not null auto_increment, name varchar(255), email varchar(255), address varchar(255), zipCode varchar(255), contactTel varchar(255), primary key (id))
生成的數據庫表結構如下:
二、數據操作
組合映射得到的表結構是一個完整的表,所以在寫入和讀取數據時采用最原始的方法就可以實現,這里還使用前幾篇文章中用到的測試方法來寫入和讀取數據,分別是使用save和load方法,具體操作見下文。
2.1 插入數據
這里使用User作為示例,Employee的寫入操作同User。在寫入數據時需要創(chuàng)建兩個對象,一個是聯系對象,另外一個是用戶對象,在保存時只需要保存用戶對象即可,相應的聯系對象會連帶著保存。
public void testSave1(){ //聲明會話對象 Session session=null; try{ //獲取會話對象 session=HibernateUtils.getSession(); //開啟會話 session.beginTransaction(); //創(chuàng)建連接對象 Contact userContact=new Contact(); userContact.setAddress("北京市"); userContact.setContactTel("1243435"); userContact.setEmail("123@gamil.com"); userContact.setZipCode("zipCode"); //創(chuàng)建用戶對象 User user=new User(); user.setName("zhangsan"); user.setUserContact(userContact); session.save(user); //提交會話 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateUtils.closeSession(session); } }
生成的SQL語句:
insert into t_user (name, email, address, zipCode, contactTel) values (?, ?, ?, ?, ?)
查看表結構如下:
2.2讀取操作
同樣使用User作為示例,Employee的操作同User對象。讀取操作相當的簡單,代碼如下:
public void testLoad1(){ //聲明會話對象 Session session=null; try{ //獲取會話對象 session=HibernateUtils.getSession(); //開啟會話 session.beginTransaction(); //獲取user對象 User user=(User)session.load(User.class, 1); System.out.println("用戶姓名: "+user.getName()); //提交會話 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateUtils.closeSession(session); } }
生成對應的結果如下:
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.email as email0_0_, user0_.address as address0_0_, user0_.zipCode as zipCode0_0_, user0_.contactTel as contactTel0_0_ from t_user user0_ where user0_.id=? 用戶姓名: zhangsan
三、綜合實例
Account:
public class Account implements Serializable{ private int id; private double money; private Address address; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Address:
public class Address implements Serializable{ private String code; private String city; private String province; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } }
Account.hbm.xml:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping package="pojo"> <class name="Account" table="t_account" > <id name="id"> <column name="id"></column> <generator class="native"></generator> </id> <property name="money"> <column name="money"></column> </property> <component name="address"> <property name="code"> <column name="code"></column> </property> <property name="city"> <column name="city"></column> </property> <property name="province"> <column name="province"></column> </property> </component> </class> </hibernate-mapping>
相關文章
使用eclipse 實現將springboot項目打成jar包
這篇文章主要介紹了使用eclipse 實現將springboot項目打成jar包的流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java timezone設置和mybatis連接數據庫時區(qū)設置方式
這篇文章主要介紹了Java timezone設置和mybatis連接數據庫時區(qū)設置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09IDEA 顯示Run Dashboard窗口的2種方式(推薦)
這篇文章主要介紹了IDEA 顯示Run Dashboard窗口的2種方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08