【Hibernate】组合映射

来源:互联网 发布:origin怎么导入数据 编辑:程序博客网 时间:2024/06/09 22:44

    上篇博客介绍了复合主键映射,这篇博客介绍组合(component)映射,组合映射关系就是把两个对象的公共部分抽象出来形成一个对象,两个子对象会包含另一个主对象。组合是关联关系的一种特殊情况,是关联关系耦合度最高的一种关系,组合的主对象和子对象拥有相同的生命周期,主对像消亡的话子对象也会消亡。这里使用雇主和用户作为示例,用户和雇主都拥有联系方式属性,抽象出来一个共同的联系方式,两种人分别包含相应的联系方式对象即可。

1.对象模型:


2.关系模型:


3.对象类和配置文件代码部分:

Employee.java,需要将contact对象包含在里面

public class Employee {private int id;private String name;private Contact 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;}public Contact getEmployeeContact() {return employeeContact;}public void setEmployeeContact(Contact employeeContact) {this.employeeContact = employeeContact;}}

User.java,同样,需要将contact对象包含其中:

public class User {private int id;private String name;private Contact userContact;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;}public Contact getUserContact() {return userContact;}public void setUserContact(Contact userContact) {this.userContact = userContact;}}

Contact.java,按照对象模型添加对应的属性即可:

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;}}

Employee.hbm.xml,添加对应的映射文件,映射的组合对象要使用<component>来标明,并在该标签中添加对应的对象属性:

<hibernate-mapping><class name="com.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></hibernate-mapping>

同理,User.hbm.xml:

<hibernate-mapping><class name="com.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>

4.插入操作代码部分:

public void testSave() {Session session = null;try {session = HibernateUtils.getSession();session.beginTransaction();//建立User实体类User user = new User();user.setName("张三");//建立Contact值类,值类通常从属于实体类Contact userContact = new Contact();userContact.setEmail("email");userContact.setAddress("address");userContact.setZipCode("zipCode");userContact.setContactTel("contactTel");user.setUserContact(userContact);session.save(user);session.getTransaction().commit();}catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();}finally {HibernateUtils.closeSession(session);}}

5.读取代码部分:

public void testLoad() {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.getTransaction().commit();}catch(Exception e) {e.printStackTrace();session.getTransaction().rollback();}finally {HibernateUtils.closeSession(session);}}

小结:

      组合映射很好理解,就是将公共部分抽象出一个新的实体,这个新的实体与它们的关系为组合关系,组合映射需要<component>标签,都挺简单的,不多说,下篇博客介绍集合映射。

0 0