在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java的Hibernate框架中的组合映射学习教程

2016-12-20 13:17| 发布者: zhangjf| 查看: 559| 评论: 0

摘要: 一、组合映射 组合是关联关系的一种特殊情况,是关联关系耦合度最高的一种关系,组合的主对象和子对象拥有相同的生命周期,主对像消亡的话子对象也会消亡。这里使用雇主和用户作为示例,用户和雇主都拥有联系方式属 ...

一、组合映射
组合是关联关系的一种特殊情况,是关联关系耦合度最高的一种关系,组合的主对象和子对象拥有相同的生命周期,主对像消亡的话子对象也会消亡。这里使用雇主和用户作为示例,用户和雇主都拥有联系方式属性,如果这里站在对象角度思考的话,常常会把对象模型绘制成为组合的方式,抽象出来一个共同的联系方式类,然后两种人分别包含相应的联系方式对象即可,向应的对象模型时它的对象示例如下图所示:

201671195324645.png (542×419)

组合对象模型在生成相应的关系模型后会把对应的子类包含到主表中,所以对应的表结构会将相应的属性生成到对应的表中,相应的表结构如下:

201671195350706.png (571×236)

1.1 Employee类及映射文件

在对象模型中Employee和Contact之间拥有包含关系,在编写代码时需要将Contact对象包含在Employee中。对应的映射文件中也需要有Contact对象的映射,需要使用标签来标明组合的对象,并把对象的属性添加到对象标签中。
清单一:Employee.java,类文件中除了基本的属性外还需要分装Contact对象,因为它们之间有一层包含关系。

  1. package com.src.hibernate;
  2. public class Employee {
  3. //id号
  4. private int id;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. //名称
  12. private String name;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. //联系对象
  20. private Contact userContact;
  21. public Contact getUserContact() {
  22. return userContact;
  23. }
  24. public void setUserContact(Contact userContact) {
  25. this.userContact = userContact;
  26. }
  27. }
复制代码

清单二:Employee.hbm.xml,添加对应的映射文件,映射的组合对象要使用来标明,并在该标签中添加对应的对象属性,具体如下代码:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="com.src.hibernate.Employee" table="t_employee">
  7. <id name="id">
  8. <generator class="native"/>
  9. </id>
  10. <property name="name"/>
  11. <component name="employeeContact">
  12. <property name="email"/>
  13. <property name="address"/>
  14. <property name="zipCode"/>
  15. <property name="contactTel"/>
  16. </component>
  17. </class>
  18. </hibernate-mapping>
复制代码

1.2 User类及配置文件

清单三:User.java,它的内容结构和Employee.java的相同,其它的不再多说,看代码:

  1. package com.src.hibernate;
  2. public class User {
  3. //id号
  4. private int id;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. //姓名
  12. private String name;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. //联系对象
  20. private Contact userContact;
  21. public Contact getUserContact() {
  22. return userContact;
  23. }
  24. public void setUserContact(Contact userContact) {
  25. this.userContact = userContact;
  26. }
  27. }
复制代码

清单四:User.hbm.xml,它的内容结构同Employee.hbm.xml内容,主要是标签的使用,很简单,代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="com.src.hibernate.User" table="t_user">
  7. <id name="id">
  8. <generator class="native"/>
  9. </id>
  10. <property name="name"/>
  11. <component name="userContact">
  12. <property name="email"/>
  13. <property name="address"/>
  14. <property name="zipCode"/>
  15. <property name="contactTel"/>
  16. </component>
  17. </class>
  18. </hibernate-mapping>
复制代码

1.3 Contact.java类
该类文件没有什么需要注意的地方,添加基本的属性即可,也不需要为该类配置对应的映射,所以它的内容相当的简单。

  1. package com.src.hibernate;
  2. public class Contact {
  3. //email地址
  4. private String email;
  5. public String getEmail() {
  6. return email;
  7. }
  8. public void setEmail(String email) {
  9. this.email = email;
  10. }
  11. //住址
  12. private String address;
  13. public String getAddress() {
  14. return address;
  15. }
  16. public void setAddress(String address) {
  17. this.address = address;
  18. }
  19. //邮编号
  20. private String zipCode;
  21. public String getZipCode() {
  22. return zipCode;
  23. }
  24. public void setZipCode(String zipCode) {
  25. this.zipCode = zipCode;
  26. }
  27. //联系电话
  28. private String contactTel;
  29. public String getContactTel() {
  30. return contactTel;
  31. }
  32. public void setContactTel(String contactTel) {
  33. this.contactTel = contactTel;
  34. }
  35. }
复制代码

1.4 生成结果
经过上面的文件配置后接下来就可以生成相应的数据库表结构了,生成的SQL语句如下:

  1. drop table if exists t_employee
  2. drop table if exists t_user
  3. 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))
  4. 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))
复制代码

生成的数据库表结构如下:

201671195556143.png (668×406)

二、数据操作
组合映射得到的表结构是一个完整的表,所以在写入和读取数据时采用最原始的方法就可以实现,这里还使用前几篇文章中用到的测试方法来写入和读取数据,分别是使用save和load方法,具体操作见下文。
2.1 插入数据
这里使用User作为示例,Employee的写入操作同User。在写入数据时需要创建两个对象,一个是联系对象,另外一个是用户对象,在保存时只需要保存用户对象即可,相应的联系对象会连带着保存。

  1. public void testSave1(){
  2. //声明会话对象
  3. Session session=null;
  4. try{
  5. //获取会话对象
  6. session=HibernateUtils.getSession();
  7. //开启会话
  8. session.beginTransaction();
  9. //创建连接对象
  10. Contact userContact=new Contact();
  11. userContact.setAddress("北京市");
  12. userContact.setContactTel("1243435");
  13. userContact.setEmail("123@gamil.com");
  14. userContact.setZipCode("zipCode");
  15. //创建用户对象
  16. User user=new User();
  17. user.setName("zhangsan");
  18. user.setUserContact(userContact);
  19. session.save(user);
  20. //提交会话
  21. session.getTransaction().commit();
  22. }catch(Exception e){
  23. e.printStackTrace();
  24. session.getTransaction().rollback();
  25. }finally{
  26. HibernateUtils.closeSession(session);
  27. }
  28. }
复制代码

生成的SQL语句:

  1. insert into t_user (name, email, address, zipCode, contactTel) values (?, ?, ?, ?, ?)
复制代码

查看表结构如下:

201671195640625.png (668×117)

2.2读取操作

同样使用User作为示例,Employee的操作同User对象。读取操作相当的简单,代码如下:

  1. public void testLoad1(){
  2. //声明会话对象
  3. Session session=null;
  4. try{
  5. //获取会话对象
  6. session=HibernateUtils.getSession();
  7. //开启会话
  8. session.beginTransaction();
  9. //获取user对象
  10. User user=(User)session.load(User.class, 1);
  11. System.out.println("用户姓名: "+user.getName());
  12. //提交会话
  13. session.getTransaction().commit();
  14. }catch(Exception e){
  15. e.printStackTrace();
  16. session.getTransaction().rollback();
  17. }finally{
  18. HibernateUtils.closeSession(session);
  19. }
  20. }
复制代码

生成对应的结果如下:

  1. 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=?
  2. 用户姓名: zhangsan
复制代码

三、综合实例
Account:

  1. public class Account implements Serializable{
  2. private int id;
  3. private double money;
  4. private Address address;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public double getMoney() {
  12. return money;
  13. }
  14. public void setMoney(double money) {
  15. this.money = money;
  16. }
  17. public Address getAddress() {
  18. return address;
  19. }
  20. public void setAddress(Address address) {
  21. this.address = address;
  22. }
  23. }
复制代码

Address:

  1. public class Address implements Serializable{
  2. private String code;
  3. private String city;
  4. private String province;
  5. public String getCode() {
  6. return code;
  7. }
  8. public void setCode(String code) {
  9. this.code = code;
  10. }
  11. public String getCity() {
  12. return city;
  13. }
  14. public void setCity(String city) {
  15. this.city = city;
  16. }
  17. public String getProvince() {
  18. return province;
  19. }
  20. public void setProvince(String province) {
  21. this.province = province;
  22. }
  23. }
复制代码

Account.hbm.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!--
  5. Mapping file autogenerated by MyEclipse Persistence Tools
  6. -->
  7. <hibernate-mapping package="pojo">
  8. <class name="Account" table="t_account" >
  9. <id name="id">
  10. <column name="id"></column>
  11. <generator class="native"></generator>
  12. </id>
  13. <property name="money">
  14. <column name="money"></column>
  15. </property>
  16. <component name="address">
  17. <property name="code">
  18. <column name="code"></column>
  19. </property>
  20. <property name="city">
  21. <column name="city"></column>
  22. </property>
  23. <property name="province">
  24. <column name="province"></column>
  25. </property>
  26. </component>
  27. </class>
  28. </hibernate-mapping>
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-19 03:45

Copyright 2015-2025 djqfx

返回顶部