如圖,當(dāng)多個(gè)對(duì)象之間有類似的屬性的時(shí)候,可以將其公用的屬性進(jìn)行抽離:

拿User和Employee對(duì)象舉例,此時(shí)User和Employee中的公用屬性已經(jīng)被抽離到一個(gè)單獨(dú)的對(duì)象“Comtact”當(dāng)中,但是此時(shí)數(shù)據(jù)表仍然如下:

由此可見,Component這種映射的方法,在對(duì)象模型中“細(xì)粒度”,在關(guān)系模型中仍然是“粗粒度”。
代碼實(shí)現(xiàn):
(1)實(shí)體
1.抽取的公共類Contact
public class Contact { PRivate String email; private String address; private String zipCode; private String contactTel; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getContactTel() { return contactTel; } public void setContactTel(String contactTel) { this.contactTel = contactTel; }} 2.User類public class User { private String id; private String name; private Contact userContact; public Contact getUserContact() { return userContact; } public void setUserContact(Contact userContact) { this.userContact = userContact; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }} 3.Employee類public class Employee { private int id; private String name; private Contact employeeContact; public Contact getEmployeeContact() { return employeeContact; } public void setEmployeeContact(Contact employeeContact) { this.employeeContact = employeeContact; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }} 由此可見,Contact單獨(dú)抽取之后,在User和Employee中,將Contact作為引用,成為其中的一個(gè)屬性,并生成對(duì)應(yīng)get和set方法。 (2).hbm.xml配置文件
<hibernate-mapping> <class name="com.bjpowernode.hibernate.Employee" table="t_emplyee"> <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> <class name="com.bjpowernode.hibernate.User" table="t_user"> <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> 由此可見,在該hbm.xml當(dāng)中,主鍵生成策略為"native",正常的name屬性之后,加入<component>關(guān)聯(lián)屬性,子標(biāo)簽中加入<email><address>等子屬性。
(3)存儲(chǔ)、加載測(cè)試
public void testSave1(){ session session = null; try{ session = HibernateUtils.getSession(); session.beginTransaction(); User user = new User(); user.setName("張三"); Contact userContact = new Contact(); userContact.setAddress("address"); userContact.setContactTel("contactTel"); userContact.setEmail("email"); userContact.setZipCode("zipCode"); user.setUserContact(userContact); session.save(user); session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); }finally{ HibernateUtils.closeSession(session); }} 分別實(shí)例化User和Contact對(duì)象并復(fù)制,將Contact對(duì)象關(guān)聯(lián)到User當(dāng)中后,直接session.save(user)即可,因?yàn)楸碇袑?shí)際只有一張表,不會(huì)因?qū)ο鬆顟B(tài)出現(xiàn)Exception問題。 同理,加載方法如下:
public void testLoad1(){ Session session = null; try{ session = HibernateUtils.getSession(); session.beginTransaction(); User user = (User)session.load(User.class, 1); System.out.println(user.getName()); System.out.println(user.getUserContact().getAddress()); session.save(user); session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); }finally{ HibernateUtils.closeSession(session); }} 通過加載User對(duì)象,即可加載出Contact中的內(nèi)容。 總結(jié):
采用Component的好處在于,實(shí)現(xiàn)對(duì)象模型的細(xì)粒度劃分,復(fù)用率更高,層次更加分明。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注