Hibernate教程04——类继承(子类、父类链接表)
2015-12-27 16:41
239 查看
Hibernate教程04——类继承(子类、父类链接表)
一、简介
每个类都生成一张表,父类保存公共数据,子类保存自己独有的数据,通过id进行关联。
父类(Animal)和子类(Pig、Bird)均生成一张表,其中父类表中存放公共的属性,子类表中分别存放各自的属性字段,子表的主键均来自主表。如下表所示:
一、父类Tree和相关annotation的配置
二、子类Department
同前
三、子类menu
同前
四、测试类
同前
五、生成的表
![](http://img.blog.csdn.net/20151227164026097?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
六、表中的数据
![](http://img.blog.csdn.net/20151227164037101?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
图 1 数据库表department中的数据
![](http://img.blog.csdn.net/20151227164049974?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
图 2 数据库表menu中的数据
![](http://img.blog.csdn.net/20151227164133535?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
图 3 数据库表tree中的数据
一、简介
每个类都生成一张表,父类保存公共数据,子类保存自己独有的数据,通过id进行关联。
父类(Animal)和子类(Pig、Bird)均生成一张表,其中父类表中存放公共的属性,子类表中分别存放各自的属性字段,子表的主键均来自主表。如下表所示:
一、父类Tree和相关annotation的配置
@Entity @Inheritance(strategy = InheritanceType.JOINED) public class Tree { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; private String name; private String description; @ManyToOne private Tree parent; @OneToMany private List<Tree> children; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Tree getParent() { return parent; } public void setParent(Tree parent) { this.parent = parent; } public List<Tree> getChildren() { return children; } public void setChildren(List<Tree> children) { this.children = children; } }
二、子类Department
同前
三、子类menu
同前
四、测试类
同前
五、生成的表
六、表中的数据
图 1 数据库表department中的数据
图 2 数据库表menu中的数据
图 3 数据库表tree中的数据
相关文章推荐
- Hadoop IO 特性详解(2)
- Eclipse下搭建C/C++开发环境
- uva 10340 - All in All
- java单元测试junit测试线程问题
- Linux网络编程之TCP协议(基础班)
- AsyncTask源码解析
- Android中MediaButtonReceiver广播监听器的机制分析
- java实现的选择排序、冒泡排序、二分查找
- Ubuntu 14.04中文输入法的安装
- 23种设计模式(14)_行为型_命令模式(Command Pattern)
- 非常全的linux面试笔试题及参考答案
- 24.利用UITableView制作一个的动态的个人信息列表
- bzoj2761 [JLOI2011]不重复数字
- html--9(IE版本测试工具及开发工具)
- 初步掌握HDFS的架构及原理3
- JavaScript 使用Document记录cookie
- Scanner类的.next()和.hashNext()方法
- USACO Humble Numbers
- Matlab生成视频文件
- AudioManager详解(结合源代码)