Hibernate映射集合属性1__Set集合

本是古典 何须时尚 2022-06-16 04:58 278阅读 0赞
  1. Set
  2. HashSet 无序,不重复
  3. List
  4. 有序,可重复
  5. Map
  6. HashMap 无序,不重复(以key为准)
  7. 数组
  8. e_user e_user_addressSet
  9. id userId
  10. name address
  11. 要说明的信息:
  12. a:集合表的名称(集合表)
  13. b:集合表中的外键(集合外键)
  14. c:集合表中的元素列(集合元素)
  15. <?xml version="1.0"?>
  16. <!DOCTYPE hibernate-mapping PUBLIC
  17. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  18. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  19. <!-- 导入包 -->
  20. <hibernate-mapping package="cn.itcast.e_hbm_collection">
  21. <!-- 类名 -->
  22. <class name="User" table="e_user">
  23. <id name="id" type="integer" column="id_">
  24. <generator class="native" />
  25. </id>
  26. <property name="name" type="string" column="name_" />
  27. <!-- addressSet属性,Set集合 -->
  28. <set name="addressSet" table="e_user_addressSet">
  29. <key column="userId"></key>
  30. <element type="string" column="address_"></element>
  31. </set>
  32. </class>
  33. </hibernate-mapping>
  34. package cn.itcast.e_hbm_collection;
  35. import java.util.HashSet;
  36. import java.util.Set;
  37. public class User {
  38. private Integer id;
  39. private String name;
  40. private Set<String> addressSet = new HashSet<String>();// Set集合
  41. public Integer getId() {
  42. return id;
  43. }
  44. public void setId(Integer id) {
  45. this.id = id;
  46. }
  47. public String getName() {
  48. return name;
  49. }
  50. public void setName(String name) {
  51. this.name = name;
  52. }
  53. public Set<String> getAddressSet() {
  54. return addressSet;
  55. }
  56. public void setAddressSet(Set<String> addressSet) {
  57. this.addressSet = addressSet;
  58. }
  59. }
  60. package cn.itcast.e_hbm_collection;
  61. import org.hibernate.Session;
  62. import org.hibernate.SessionFactory;
  63. import org.hibernate.Transaction;
  64. import org.hibernate.cfg.Configuration;
  65. import org.junit.Test;
  66. public class App {
  67. private static SessionFactory sessionFactory = new Configuration()//
  68. .configure()//
  69. .addClass(User.class)//
  70. .buildSessionFactory();
  71. @Test
  72. public void testSave() throws Exception {
  73. Session session = sessionFactory.openSession();
  74. Transaction tx = null;
  75. try {
  76. tx = session.beginTransaction();
  77. // ------------------------------------
  78. // 操作
  79. // Set<String> set = new HashSet<String>();
  80. // set.add("骆家庄东苑");
  81. // set.add("文一新西路");
  82. // 构建对象
  83. User user = new User();
  84. user.setName("李四");
  85. user.getAddressSet().add("新青年厂场");
  86. user.getAddressSet().add("新塘路");
  87. // 保存
  88. session.save(user);
  89. // ------------------------------------
  90. tx.commit();
  91. } catch (RuntimeException e) {
  92. tx.rollback();
  93. throw e;
  94. } finally {
  95. session.close();
  96. }
  97. }
  98. @Test
  99. public void testGet() throws Exception {
  100. Session session = sessionFactory.openSession();
  101. Transaction tx = null;
  102. try {
  103. tx = session.beginTransaction();
  104. // ------------------------------------
  105. // 获取数据
  106. User user = (User) session.get(User.class, 2);
  107. // 显示信息
  108. System.out.println(user.getId());
  109. System.out.println(user.getName());
  110. for (String s : user.getAddressSet()) {
  111. System.out.println(s);
  112. }
  113. // ------------------------------------
  114. tx.commit();
  115. } catch (RuntimeException e) {
  116. tx.rollback();
  117. throw e;
  118. } finally {
  119. session.close();
  120. }
  121. }
  122. }

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读