Hibernate继承映射分析
发布时间:2021-11-23 16:03:18 所属栏目:教程 来源:互联网
导读:在面向对象的程序领域中,类与类之间是有继承关系的,例如Java世界中只需要extends关键字就可以确定这两个类的父子关系,但是在关系数据库的世界中,表与表之间没有任何关键字可以明确指明这两张表的父子关系,表与表是没有继承关系这样的说法的。为了将程序
在面向对象的程序领域中,类与类之间是有继承关系的,例如Java世界中只需要extends关键字就可以确定这两个类的父子关系,但是在关系数据库的世界中,表与表之间没有任何关键字可以明确指明这两张表的父子关系,表与表是没有继承关系这样的说法的。为了将程序领域中的继承关系反映到数据中,Hibernate为我们提供了3中方案: 第一种方案:一个子类对应一张表。 第二种方案:使用一张表表示所有继承体系下的类的属性的并集。 第三种方案:每个子类使用一张表只存储它特有的属性,然后与父类所对应的表以一对一主键关联的方式关联起来。 现在假设有People、Student、Teacher三个类,父类为People,Student与Teacher为People的父类,代码如下: People类: public class People { /*父类所拥有的属性*/ private String id; private String name; private String sex; private String age; private Timestamp birthday; /*get和set方法*/ } Student类: public class Student extends People { /*学生独有的属性*/ private String cardId;//学号 public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } } Teacher类: public class Teacher extends People { /*Teacher所独有的属性*/ private int salary;//工资 public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } } (编辑:济南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |