Hibernate_映射_继承结构映射2_另外两种映射方式

╰半夏微凉° 2022-06-16 08:07 283阅读 0赞
  1. package cn.itcast.j_hbm_extends;
  2. import java.util.Date;
  3. /**
  4. * 文章
  5. *
  6. * @author 风清杨
  7. * @version V1.0
  8. */
  9. public class Article {
  10. private Integer id;
  11. private String title;
  12. private String content;
  13. private Date postTime;
  14. public Integer getId() {
  15. return id;
  16. }
  17. public void setId(Integer id) {
  18. this.id = id;
  19. }
  20. public String getTitle() {
  21. return title;
  22. }
  23. public void setTitle(String title) {
  24. this.title = title;
  25. }
  26. public String getContent() {
  27. return content;
  28. }
  29. public void setContent(String content) {
  30. this.content = content;
  31. }
  32. public Date getPostTime() {
  33. return postTime;
  34. }
  35. public void setPostTime(Date postTime) {
  36. this.postTime = postTime;
  37. }
  38. }
  39. package cn.itcast.j_hbm_extends;
  40. /**
  41. * 主题
  42. *
  43. * @author 风清杨
  44. * @version V1.0
  45. */
  46. public class Topic extends Article {
  47. private Integer type;// 精华、置顶...
  48. public Integer getType() {
  49. return type;
  50. }
  51. public void setType(Integer type) {
  52. this.type = type;
  53. }
  54. }
  55. package cn.itcast.j_hbm_extends;
  56. /**
  57. * 回贴
  58. *
  59. * @author 风清杨
  60. * @version V1.0
  61. */
  62. public class Reply extends Article {
  63. private Integer floor;// 楼层
  64. public Integer getFloor() {
  65. return floor;
  66. }
  67. public void setFloor(Integer floor) {
  68. this.floor = floor;
  69. }
  70. }
  71. package cn.itcast.j_hbm_extends;
  72. import org.hibernate.Session;
  73. import org.hibernate.SessionFactory;
  74. import org.hibernate.Transaction;
  75. import org.hibernate.cfg.Configuration;
  76. import org.junit.Test;
  77. /**
  78. * 应用程序操作类
  79. *
  80. * @author 风清杨
  81. * @version V1.0
  82. *
  83. */
  84. public class App {
  85. private static SessionFactory sessionFactory = new Configuration()//
  86. .configure()//
  87. .addClass(Article.class)//
  88. .buildSessionFactory();
  89. // 保存,有关联关系
  90. @Test
  91. public void testSave() throws Exception {
  92. Session session = sessionFactory.openSession();
  93. Transaction tx = null;
  94. try {
  95. tx = session.beginTransaction();
  96. // ------------------------------------
  97. // 新键对象
  98. Article article = new Article();
  99. article.setTitle("这是一个Article");
  100. Topic topic = new Topic();
  101. topic.setTitle("这是一个Topic");
  102. Reply reply = new Reply();
  103. reply.setTitle("这是一个Reply");
  104. // 保存
  105. session.save(article);
  106. session.save(topic);
  107. session.save(reply);
  108. // ------------------------------------
  109. tx.commit();
  110. } catch (RuntimeException e) {
  111. tx.rollback();
  112. throw e;
  113. } finally {
  114. session.close();
  115. }
  116. }
  117. // 获取,可以获取到关联的对方
  118. @Test
  119. public void testGet() throws Exception {
  120. Session session = sessionFactory.openSession();
  121. Transaction tx = null;
  122. try {
  123. tx = session.beginTransaction();
  124. // ------------------------------------
  125. // 获取
  126. Article article = (Article) session.get(Article.class, 1);
  127. Topic topic = (Topic) session.get(Topic.class, 2);
  128. Reply reply = (Reply) session.get(Reply.class, 3);
  129. System.out.println(article);
  130. System.out.println(topic);
  131. System.out.println(reply);
  132. System.out.println();
  133. Article article1 = (Article) session.get(Article.class, 2);
  134. Article article2 = (Article) session.get(Article.class, 3);
  135. System.out.println(article1);
  136. System.out.println(article2);
  137. // ------------------------------------
  138. tx.commit();
  139. } catch (RuntimeException e) {
  140. tx.rollback();
  141. throw e;
  142. } finally {
  143. session.close();
  144. }
  145. }
  146. }

方式1

Center

  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. <!-- 导入包 -->
  6. <hibernate-mapping package="cn.itcast.j_hbm_extends2">
  7. <!-- 采用每个类一张表的方式,抽象类也对应表 -->
  8. <class name="Article" table="t_article">
  9. <id name="id" type="integer" column="id_">
  10. <generator class="native" />
  11. </id>
  12. <property name="title" type="string" column="title_" />
  13. <property name="content" type="text" length="10000" column="content_"></property>
  14. <property name="postTime" type="timestamp" column="postTime_"></property>
  15. <!-- 子类:Topic -->
  16. <joined-subclass name="Topic" table="t_topic">
  17. <key column="articleId"></key>
  18. <property name="type" type="integer" column="type_"/>
  19. </joined-subclass>
  20. <!-- 子类:Reply -->
  21. <joined-subclass name="Reply" table="t_reply">
  22. <key column="articleId"></key>
  23. <property name="floor" type="integer" column="floor_"/>
  24. </joined-subclass>
  25. </class>
  26. </hibernate-mapping>

方式2

Center 1

  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. <!-- 导入包 -->
  6. <hibernate-mapping package="cn.itcast.j_hbm_extends3">
  7. <!-- 采用每个具体类一张表的方式,抽象类不对应表
  8. abstract默认为false,设为true表示本类不对应表(类可以不是abstract的),这时就会忽略table属性。
  9. -->
  10. <class name="Article" abstract="false">
  11. <id name="id" type="integer" column="id_">
  12. <!--
  13. 当使用每个具体类一张表的方式时,主键生成策略不能是identity。
  14. 因为在整个继承结构中,主键值是不能重复的。
  15. -->
  16. <generator class="hilo">
  17. <param name="table">hi_value</param>
  18. <param name="column">next_value</param>
  19. <param name="max_lo">100</param>
  20. </generator>
  21. </id>
  22. <property name="title" type="string" column="title_" />
  23. <property name="content" type="text" length="10000" column="content_"></property>
  24. <property name="postTime" type="timestamp" column="postTime_"></property>
  25. <!-- 子类:Topic -->
  26. <union-subclass name="Topic" table="t_topic">
  27. <property name="type" type="integer" column="type_"/>
  28. </union-subclass>
  29. <!-- 子类:Reply -->
  30. <union-subclass name="Reply" table="t_reply">
  31. <property name="floor" type="integer" column="floor_"/>
  32. </union-subclass>
  33. </class>
  34. </hibernate-mapping>

发表评论

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

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

相关阅读

    相关 hibernate映射方式

    hibernate之映射的两种方式 将实体类映射到数据库中的表的方式有两种,包括通过xml文件映射以及通过注解映射。个人认为通过注解映射更加直观,出错率低一些吧。

    相关 hibernate映射方式

    hibernate之映射的两种方式 将实体类映射到数据库中的表的方式有两种,包括通过xml文件映射以及通过注解映射。个人认为通过注解映射更加直观,出错率低一些吧。