当前位置:Gxlcms > mysql > Hibernate关联关系映射之继承映射

Hibernate关联关系映射之继承映射

时间:2021-07-01 10:21:17 帮助过:22人阅读

首先有一个文章类(Article)类中有id、title、content、postTime等属性。 package entity;import java.util.Date;public class Article {private Integer id;private String title;private String content;private Date postTime;public Integer getId() {r

首先有一个文章类(Article)类中有id、title、content、postTime等属性。

  1. package entity;
  2. import java.util.Date;
  3. public class Article {
  4. private Integer id;
  5. private String title;
  6. private String content;
  7. private Date postTime;
  8. public Integer getId() {
  9. return id;
  10. }
  11. public void setId(Integer id) {
  12. this.id = id;
  13. }
  14. public String getTitle() {
  15. return title;
  16. }
  17. public void setTitle(String title) {
  18. this.title = title;
  19. }
  20. public String getContent() {
  21. return content;
  22. }
  23. public void setContent(String content) {
  24. this.content = content;
  25. }
  26. public Date getPostTime() {
  27. return postTime;
  28. }
  29. public void setPostTime(Date postTime) {
  30. this.postTime = postTime;
  31. }
  32. }

然后看他的子类,Topic类和Reply类。他们除了父类的属性外还有自己独特的属性。

  1. package entity;
  2. public class Topic extends Article{
  3. private int type;
  4. public int getType() {
  5. return type;
  6. }
  7. public void setType(int type) {
  8. this.type = type;
  9. }
  10. }
  1. package entity;
  2. public class Reply extends Article{
  3. private int floor;
  4. public int getFloor() {
  5. return floor;
  6. }
  7. public void setFloor(int floor) {
  8. this.floor = floor;
  9. }
  10. }

继承映射的方式有三种方式,我们一个个看:

方式一:将父类和子类的信息存放在同一个表中,然后在该数据表中有个字段用来表示该条记录的类型,其中子类独有的属性允许为空。我们看一下映射配置文件。

  1. <!--?xml version="1.0" encoding="utf-8"?-->
  2. <hibernate-mapping package="entity">
  3. <class name="entity.Article" table="article" schema="MYHR" discriminator-value="Article">
  4. <id name="id" type="int">
  5. <column name="ID">
  6. <generator class="assigned">
  7. </generator></column></id>
  8. <!-- 用于鉴别是什么类型的一个列 -->
  9. <discriminator type="string" column="t_class">
  10. <property name="title">
  11. <property name="content" type="clob" length="5000">
  12. <property name="postTime" type="timestamp">
  13. <subclass name="Topic" discriminator-value="Topic">
  14. <property name="type" type="int">
  15. </property></subclass>
  16. <subclass name="Reply" discriminator-value="Reply">
  17. <property name="floor" type="int">
  18. </property></subclass>
  19. </property></property></property></discriminator></class>
  20. </hibernate-mapping>

方式二:父类和子类不在同一张表中,且每一个类一张表,抽象类对应一张表,这是配置子类使用joined-subclass

  1. <!--?xml version="1.0" encoding="utf-8"?-->
  2. <hibernate-mapping package="entity">
  3. <class name="entity.Article" table="article" schema="MYHR">
  4. <id name="id" type="int">
  5. <column name="ID">
  6. <generator class="assigned">
  7. </generator></column></id>
  8. <property name="title">
  9. <property name="content" type="clob" length="5000">
  10. <property name="postTime" type="timestamp">
  11. <joined-subclass name="Topic" table="topic">
  12. <key column="id">
  13. <property name="type" type="int">
  14. </property></key></joined-subclass>
  15. <joined-subclass name="Reply" table="reply">
  16. <key column="id">
  17. <property name="floor" type="int">
  18. </property></key></joined-subclass>
  19. </property></property></property></class>
  20. </hibernate-mapping>

方式三:每一个类单独一张表,并且抽象类不对应一张表,子类对应的数据库表中对应全部的属性,包括从父类继承的信息。

  1. <!--?xml version="1.0" encoding="utf-8"?-->
  2. <hibernate-mapping package="entity">
  3. <!-- 采用每个具体类一张表,抽象类不对应表,abstract默认为false,设为true表示为抽象的不对应表 -->
  4. <class name="entity.Article" abstract="true" schema="MYHR">
  5. <id name="id" type="int">
  6. <column name="ID">
  7. <generator class="assigned">
  8. </generator></column></id>
  9. <property name="title">
  10. <property name="content" type="clob" length="5000">
  11. <property name="postTime" type="timestamp">
  12. <union-subclass name="Topic" table="topic">
  13. <property name="type" type="int">
  14. </property></union-subclass>
  15. <union-subclass name="Reply" table="reply">
  16. <property name="floor" type="int">
  17. </property></union-subclass>
  18. </property></property></property></class>
  19. </hibernate-mapping>

然后看一下测试类,三种方式测试类相同,只有方式三的save方法中因为抽象类没有单独的数据表因此不能存储他们父类的信息。

  1. package test;
  2. import org.hibernate.Session;
  3. import org.hibernate.Transaction;
  4. import entity.Article;
  5. import entity.Reply;
  6. import entity.Topic;
  7. import factory.HibernateSessionFactory;
  8. public class Test {
  9. private Session session = null;
  10. private Transaction tran = null;
  11. @org.junit.Test
  12. public void save() {
  13. session = HibernateSessionFactory.getSession();
  14. tran = session.beginTransaction();
  15. try{
  16. Article article = new Article();
  17. article.setId(1);
  18. article.setTitle("这是一个Article");
  19. Topic topic = new Topic();
  20. topic.setId(2);
  21. topic.setTitle("这是一个Topic");
  22. Reply reply = new Reply();
  23. reply.setId(3);
  24. reply.setTitle("这是一个reply");
  25. session.save(article);
  26. session.save(topic);
  27. session.save(reply);
  28. tran.commit();
  29. }catch(Exception e){
  30. tran.rollback();
  31. }
  32. }
  33. @org.junit.Test
  34. public void Get() {
  35. session = HibernateSessionFactory.getSession();
  36. tran = session.beginTransaction();
  37. try{
  38. Article a = (Article)session.get(Article.class, 1);
  39. Topic t = (Topic)session.get(Topic.class, 2);
  40. Reply r = (Reply)session.get(Reply.class, 3);
  41. System.out.println(a.getTitle());
  42. System.out.println(t.getTitle());
  43. System.out.println(r.getTitle());
  44. tran.commit();
  45. }catch(Exception e){
  46. tran.rollback();
  47. }
  48. }
  49. }


人气教程排行