您的位置:首页 > 其它

Hibernate教程04——类继承(子类、父类链接表)

2015-12-27 16:41 239 查看
Hibernate教程04——类继承(子类、父类链接表)

一、简介

每个类都生成一张表,父类保存公共数据,子类保存自己独有的数据,通过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中的数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: