一、普通组件映射配置

1、创建组件类,代码如下:

  1. package learn.hibernate.bean;
  2.  
  3. /**
  4. * 组件类
  5. */
  6. public class Phones {
  7.  
  8. private String companyPhone;
  9. private String homePhone;
  10. private String personalPhone;
  11.  
  12. public Phones() {
  13.  
  14. }
  15.  
  16. public Phones(String companyPhone, String homePhone, String personalPhone) {
  17. super();
  18. this.companyPhone = companyPhone;
  19. this.homePhone = homePhone;
  20. this.personalPhone = personalPhone;
  21. }
  22.  
  23. @Override
  24. public String toString() {
  25. return "Phones [companyPhone=" + companyPhone + ", homePhone="
  26. + homePhone + ", personalPhone=" + personalPhone + "]";
  27. }
  28.  
  29. public String getCompanyPhone() {
  30. return companyPhone;
  31. }
  32. public void setCompanyPhone(String companyPhone) {
  33. this.companyPhone = companyPhone;
  34. }
  35. public String getHomePhone() {
  36. return homePhone;
  37. }
  38. public void setHomePhone(String homePhone) {
  39. this.homePhone = homePhone;
  40. }
  41. public String getPersonalPhone() {
  42. return personalPhone;
  43. }
  44. public void setPersonalPhone(String personalPhone) {
  45. this.personalPhone = personalPhone;
  46. }
  47. }
  1. package learn.hibernate.bean;
  2.  
  3. /**
  4. * 组件类
  5. */
  6. public class Address {
  7.  
  8. private String zipCode;
  9. private String address;
  10.  
  11. public Address() {
  12. super();
  13. }
  14.  
  15. public Address(String zipCode, String address) {
  16. super();
  17. this.zipCode = zipCode;
  18. this.address = address;
  19. }
  20.  
  21. @Override
  22. public String toString() {
  23. return "Address [zipCode=" + zipCode + ", address=" + address + "]";
  24. }
  25. public String getZipCode() {
  26. return zipCode;
  27. }
  28. public void setZipCode(String zipCode) {
  29. this.zipCode = zipCode;
  30. }
  31. public String getAddress() {
  32. return address;
  33. }
  34. public void setAddress(String address) {
  35. this.address = address;
  36. }
  37. }

2、将组件类作为属性放入到主类中,代码如下:

  1. package learn.hibernate.bean;
  2.  
  3. import java.util.Date;
  4.  
  5. /**
  6. * 持久化类
  7. */
  8. public class Person {
  9.  
  10. private Integer id;
  11. private String name;
  12. private int age;
  13. private int passwork;
  14. private Date birthday;
  15. // 组件实例
  16. private Address addres;
  17. // 组件实例
  18. private Phones phone;
  19.  
  20. public Person() {
  21.  
  22. }
  23.  
  24. public Person(String name, int age, int passwork, Date birthday) {
  25. super();
  26. this.name = name;
  27. this.age = age;
  28. this.passwork = passwork;
  29. this.birthday = birthday;
  30. }
  31.  
  32. @Override
  33. public String toString() {
  34. return "Person [id=" + id + ", name=" + name + ", age=" + age
  35. + ", passwork=" + passwork + ", birthday=" + birthday + "]";
  36. }
  37.  
  38. public Integer getId() {
  39. return id;
  40. }
  41. public void setId(Integer id) {
  42. this.id = id;
  43. }
  44. public String getName() {
  45. return name;
  46. }
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50. public int getAge() {
  51. return age;
  52. }
  53. public void setAge(int age) {
  54. this.age = age;
  55. }
  56. public int getPasswork() {
  57. return passwork;
  58. }
  59. public void setPasswork(int passwork) {
  60. this.passwork = passwork;
  61. }
  62. public Date getBirthday() {
  63. return birthday;
  64. }
  65. public void setBirthday(Date birthday) {
  66. this.birthday = birthday;
  67. }
  68.  
  69. public Address getAddres() {
  70. return addres;
  71. }
  72.  
  73. public void setAddres(Address addres) {
  74. this.addres = addres;
  75. }
  76.  
  77. public Phones getPhone() {
  78. return phone;
  79. }
  80.  
  81. public void setPhone(Phones phone) {
  82. this.phone = phone;
  83. }
  84.  
  85. }

3、配置文件,代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="learn.hibernate.bean">
  6. <class name="Person" table="t_person">
  7. <id name="id" column="person_id">
  8. <generator class="native"/>
  9. </id>
  10. <property name="name" column="t_name"/>
  11. <property name="age"/>
  12. <property name="passwork"/>
  13. <property name="birthday"/>
  14. <!--
  15. 组件类的映射配置
  16. component 指定需要映射的组件类
  17. name 指定 Person 中组件属性的变量名称
  18. name的值"addres" 与 Person 中定义的要一致
  19. -->
  20. <component name="addres">
  21. <property name="zipCode"/>
  22. <property name="address"/>
  23. </component>
  24.  
  25. <component name="phone">
  26. <property name="companyPhone"/>
  27. <property name="homePhone"/>
  28. <property name="personalPhone"/>
  29. </component>
  30. </class>
  31. </hibernate-mapping>

4、测试代码:

  1. @Test
  2. public void testComponent() {
  3. Person p = new Person("sdf",23,123456,new Date());
  4. Address address = new Address("410000","湖南长沙");
  5. Phones phone = new Phones("07318678987","0731876567","15114565678");
  6. // person 与 address 关联
  7. p.setAddres(address);
  8. // person 与 phone 关联
  9. p.setPhone(phone);
  10.  
  11. tx = session.beginTransaction();
  12.  
  13. session.persist(p);
  14.  
  15. tx.commit();
  16.  
  17. }

二、动态组件映射配置

1、创建类,代码如下:

  1. package learn.hibernate.bean;
  2.  
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. /**
  8. * 持久化类设计
  9. * 注意:
  10. * 持久化类通常建议要有一个持久化标识符(ID)
  11. * 持久化标识符通常建议使用封装类(例如:Integer 因为基本类型存在默认值)
  12. * 持久化类通常建议手动添加一个无参构造函数 (因为有些操作是通过放射机制进行的)
  13. * 属性通常建议提供 getter/setter 方法
  14. * 持久化类不能使用 final 修饰
  15. * 持久化类中如果使用了集合类型数据,只能使用集合所对应的接口类型来声明(List/Map/Set)
  16. * 如下:ArrayList list = new ArrayList(); 不行
  17. * List list = new ArrayList(); 可行
  18. */
  19. public class Person {
  20.  
  21. private Integer id;
  22. private String name;
  23. private int age;
  24. private int passwork;
  25. private Date birthday;
  26. // 动态组件实例
  27. private Map attribute = new HashMap();
  28.  
  29. public Person() {
  30.  
  31. }
  32.  
  33. public Person(String name, int age, int passwork, Date birthday) {
  34. super();
  35. this.name = name;
  36. this.age = age;
  37. this.passwork = passwork;
  38. this.birthday = birthday;
  39. }
  40.  
  41. @Override
  42. public String toString() {
  43. return "Person [id=" + id + ", name=" + name + ", age=" + age
  44. + ", passwork=" + passwork + ", birthday=" + birthday + "]";
  45. }
  46.  
  47. public Integer getId() {
  48. return id;
  49. }
  50. public void setId(Integer id) {
  51. this.id = id;
  52. }
  53. public String getName() {
  54. return name;
  55. }
  56. public void setName(String name) {
  57. this.name = name;
  58. }
  59. public int getAge() {
  60. return age;
  61. }
  62. public void setAge(int age) {
  63. this.age = age;
  64. }
  65. public int getPasswork() {
  66. return passwork;
  67. }
  68. public void setPasswork(int passwork) {
  69. this.passwork = passwork;
  70. }
  71. public Date getBirthday() {
  72. return birthday;
  73. }
  74. public void setBirthday(Date birthday) {
  75. this.birthday = birthday;
  76. }
  77.  
  78. public Map getAttribute() {
  79. return attribute;
  80. }
  81.  
  82. public void setAttribute(Map attribute) {
  83. this.attribute = attribute;
  84. }
  85.  
  86. }

2、映射配置文件,代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="learn.hibernate.bean">
  6. <class name="Person" table="t_person">
  7. <id name="id" column="person_id">
  8. <generator class="native"/>
  9. </id>
  10. <property name="name" column="t_name"/>
  11. <property name="age"/>
  12. <property name="passwork"/>
  13. <property name="birthday"/>
  14. <!--
  15. 动态组件类的映射配置
  16. dynamic-component 指定需要映射的组件类
  17. name="attribute" 对应持久化类中集合的变量名称
  18. property Map 集合中key映射配置
  19. name 对 Map 集合的key
  20. column 存储 key 所对应的值
  21. type 字段的数据类型
  22. -->
  23. <dynamic-component name="attribute">
  24. <property name="key1" column="t_key1" type="string"/>
  25. <property name="key2" column="t_key1" type="integer"/>
  26. </dynamic-component>
  27. </class>
  28. </hibernate-mapping>

3、测试代码:

  1. @Test
  2. public void testComponent() {
  3. Person p = new Person("sdf",23,123456,new Date());
  4. // 在 Person 中只有声明,也有创建
  5. Map attribute = p.getAttribute();
  6. attribute.put("key1", "hibernate");
  7. attribute.put("key2", 123);
  8.  
  9. tx = session.beginTransaction();
  10.  
  11. session.persist(p);
  12.  
  13. tx.commit();
  14.  
  15. }

4、如果在 Person 类中只声明了动态组件,并未创建,如下:

  1. package learn.hibernate.bean;
  2.  
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. /**
  8. * 持久化类设计
  9. * 注意:
  10. * 持久化类通常建议要有一个持久化标识符(ID)
  11. * 持久化标识符通常建议使用封装类(例如:Integer 因为基本类型存在默认值)
  12. * 持久化类通常建议手动添加一个无参构造函数 (因为有些操作是通过放射机制进行的)
  13. * 属性通常建议提供 getter/setter 方法
  14. * 持久化类不能使用 final 修饰
  15. * 持久化类中如果使用了集合类型数据,只能使用集合所对应的接口类型来声明(List/Map/Set)
  16. * 如下:ArrayList list = new ArrayList(); 不行
  17. * List list = new ArrayList(); 可行
  18. */
  19. public class Person {
  20.  
  21. private Integer id;
  22. private String name;
  23. private int age;
  24. private int passwork;
  25. private Date birthday;
  26. // 动态组件实例
  27. private Map attribute;
  28.  
  29. public Person() {
  30.  
  31. }
  32.  
  33. public Person(String name, int age, int passwork, Date birthday) {
  34. super();
  35. this.name = name;
  36. this.age = age;
  37. this.passwork = passwork;
  38. this.birthday = birthday;
  39. }
  40.  
  41. @Override
  42. public String toString() {
  43. return "Person [id=" + id + ", name=" + name + ", age=" + age
  44. + ", passwork=" + passwork + ", birthday=" + birthday + "]";
  45. }
  46.  
  47. public Integer getId() {
  48. return id;
  49. }
  50. public void setId(Integer id) {
  51. this.id = id;
  52. }
  53. public String getName() {
  54. return name;
  55. }
  56. public void setName(String name) {
  57. this.name = name;
  58. }
  59. public int getAge() {
  60. return age;
  61. }
  62. public void setAge(int age) {
  63. this.age = age;
  64. }
  65. public int getPasswork() {
  66. return passwork;
  67. }
  68. public void setPasswork(int passwork) {
  69. this.passwork = passwork;
  70. }
  71. public Date getBirthday() {
  72. return birthday;
  73. }
  74. public void setBirthday(Date birthday) {
  75. this.birthday = birthday;
  76. }
  77.  
  78. public Map getAttribute() {
  79. return attribute;
  80. }
  81.  
  82. public void setAttribute(Map attribute) {
  83. this.attribute = attribute;
  84. }
  85.  
  86. }

5、那么在测试代码需要创建一个,代码如下:

  1. @Test
  2. public void testAttribute() {
  3. Person p = new Person("sdf",23,123456,new Date());
  4. // 在 Person 中只有声明,没有创建
  5. Map attribute = new HashMap();
  6. attribute.put("key1", "hibernate");
  7. attribute.put("key2", 123);
  8. p.setAttribute(attribute);
  9.  
  10. tx = session.beginTransaction();
  11.  
  12. session.persist(p);
  13.  
  14. tx.commit();
  15.  
  16. }

6、查询,测试代码:

  1. @Test
  2. public void testGetAttribute() {
  3. Person p = (Person)session.get(Person.class, 1);
  4. System.out.println(p);
  5.  
  6. // 高效操作 map 集合
  7. Iterator<Map.Entry> it = p.getAttribute().entrySet().iterator();
  8.  
  9. for(;it.hasNext();){
  10. Map.Entry map = it.next();
  11. System.out.println(map.getKey()+"---------"+map.getValue());
  12. }
  13. }

Hibernate学习---第五节:普通组件和动态组件的更多相关文章

  1. VUE2.0实现购物车和地址选配功能学习第五节

    第五节 单件商品金额计算和单选全选功能 1.vue精髓在于操作data模型来改变dom,渲染页面,而不是直接去改变dom 2.加减改变总金额功能: html:<div class="c ...

  2. Vue组件的操作-自定义组件,动态组件,递归组件

    作者 | Jeskson 来源 | 达达前端小酒馆 v-model双向绑定 创建双向数据绑定,v-model指令用来在input,select,checkbox,radio等表单控件.v-model指 ...

  3. Vue两种组件类型介绍:递归组件和动态组件

    一递归组件 递归组件的特性就是可以在自己的template模板中调用自己本身.值得注意的它必须设置name属性. // 递归组件 recursive.vue <template> < ...

  4. [Vue]组件——实现动态组件:keep-alive的使用

    1.在app.vue中用一个 <keep-alive> 元素将其动态组件包裹起来: keepAlive为true时,第一次被创建的时候缓存下来,为false时,不会缓存 <keep- ...

  5. Vue 组件4 动态组件

    动态组件 通过使用保留的<component>元素,动态的绑定到它的is特性,我们让多个组件同时使用同一个挂载点,并动态切换: var vm = new Vue({ el: '#examp ...

  6. Vue_(组件通讯)动态组件结合keep-alive

    keep-alive 传送门 <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们.和 <transition> 相似,<keep-alive ...

  7. Vue_(组件通讯)动态组件

    动态组件 传送门 在一个元素上挂载多个组件,根据不同状态进行切换的时候,可以使用动态组件 动态组件的使用:需要使用内置组件<component></component>,根据 ...

  8. Vue.js的组件(slot/动态组件等)、单文件组件、递归组件使用

    一.组件 1> 组件命名方式有两种(注意:在DOM模板中只有kebab-case命名方法才生效): html中引用组件: <!-- 在DOM模板中,只有 kebab-case命名才生效 - ...

  9. Hibernate征途(五)之继承映射和组件映射

    之所以把这两种映射放到一起说,是因为二者都是以复用为目的,减少了代码和配置量,这是相同点:二者之间的不同点类似继承和实现的区别:继承的类是一个事物的抽象,而实现的接口仅仅是功能的抽象. 继承映射 如上 ...

随机推荐

  1. IEF could not decode Chinese character in IE history well

    My friend is working on some case, and she looks not in the mood. I ask her what's going on. She wan ...

  2. Spring MVC防御CSRF、XSS和SQL注入攻击

    参考: http://www.myhack58.com/Article/html/3/7/2012/36142_6.htm http://blog.csdn.net/jasontome/article ...

  3. SQL笔记-第一章,数据库入门

    DBMS的分类DB2.Oracle.Microsoft SQL Server.Sybase SQLServer.Informix.MySQL数据库的结构元素库 database表 table列 col ...

  4. 字符串匹配KMP算法

    1. 字符串匹配的KMP算法 2. KMP算法详解 3. 从头到尾彻底理解KMP

  5. CSS中属性position位置详解功能讲解与实例分析

    position有五个值:static.relative.absolute.fixed.inherit. static 是默认值.就是按正常的布局流从上到下从左到右布局,平常我们做网页制作时,没有指定 ...

  6. CentOS6.4安装Smokeping节点监控软件

    Smokeping:它是rrdtool的作者制作的,在图形显示方面很漂亮,可以用来很好的检测网络状态和稳定性,下面简单说一下Smokeping的安装以及配置方法. 0.首先关闭selinux和防火墙 ...

  7. HTML 5 Canvas

    HTML 5 Canvas   HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. <canvas id="myCanvas" width=&q ...

  8. C# 运行时编辑 节点重命名

    方法一: ; bool nodeChanged = false; //右键点击,就进入修改状态 private void treeView1_NodeMouseClick(object sender, ...

  9. 关于SQL表联接

    以SQL2008为例,Microsoft SQL Server 2008支持四种表运算符-JOIN,APPLY,PIVOT,UNPIVOT.JOIN表运算符是ANSI标准,而其他三种是T-SQL对标准 ...

  10. while循环中不支持循环使用curl

    <?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...