首页 > 代码库 > 转Hibernate继承
转Hibernate继承
hibernate继承映射
以下测试是在mysql中进行的。
1、单表方式
Animal.java
Java代码
- @Entity
- @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
- @DiscriminatorColumn(name="animalType")
- @DiscriminatorValue("animal")
- public class Animal {
- private int id;
- private String name;
- @Id
- @GeneratedValue
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Cat.java
Java代码
- @Entity
- @DiscriminatorValue("cat")
- public class Cat extends Animal {
- private String catName;
- public String getCatName() {
- return catName;
- }
- public void setCatName(String catName) {
- this.catName = catName;
- }
- }
Dog.java
Java代码
- @Entity
- @DiscriminatorValue(value=http://www.mamicode.com/"dog")
- public class Dog extends Animal{
- private String dogName;
- public String getDogName() {
- return dogName;
- }
- public void setDogName(String dogName) {
- this.dogName = dogName;
- }
- }
TestJunit.java
Java代码
- @Test
- public void testAnimal(){
- Animal t = new Animal();
- t.setName("animal");
- session.saveOrUpdate(t);
- }
- @Test
- public void testCat(){
- Cat t = new Cat();
- t.setName("cat");
- t.setCatName("cat");
- session.saveOrUpdate(t);
- }
- @Test
- public void testDog(){
- Dog t = new Dog();
- t.setName("dog");
- t.setDogName("dog");
- session.saveOrUpdate(t);
- }
对应生成的数据表是这样的
table---animal
id | name | animalType | catName | dogName |
1 | animal | animal | null | null |
2 | cat | cat | cat | null |
3 | dog | dog | null | dog |
2、join方式
Animal.java
Java代码
- @Entity
- @Inheritance(strategy=InheritanceType.JOINED)
- public class Animal {
- private int id;
- private String name;
- @Id
- @GeneratedValue
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Cat.java
Java代码
- @Entity
- @PrimaryKeyJoinColumn(name="catId")
- public class Cat extends Animal {
- private String catName;
- public String getCatName() {
- return catName;
- }
- public void setCatName(String catName) {
- this.catName = catName;
- }
- }
Dog.java
Java代码
- @Entity
- @PrimaryKeyJoinColumn(name="dogId")
- public class Dog extends Animal{
- private String dogName;
- public String getDogName() {
- return dogName;
- }
- public void setDogName(String dogName) {
- this.dogName = dogName;
- }
- }
TestJunit.java
Java代码
- @Test
- public void testAnimal(){
- Animal t = new Animal();
- t.setName("animal");
- session.saveOrUpdate(t);
- }
- @Test
- public void testCat(){
- Cat t = new Cat();
- t.setName("cat");
- t.setCatName("cat");
- session.saveOrUpdate(t);
- }
- @Test
- public void testDog(){
- Dog t = new Dog();
- t.setName("dog");
- t.setDogName("dog");
- session.saveOrUpdate(t);
- }
对应生成的数据表是这样的
table---Animal
id | name |
1 | animal |
2 | cat |
3 | dog |
table---cat
catId | catName |
2 | cat |
table---dog
dogId | dogName |
3 | dog |
3、每个实体类对应一个数据表
以下的测试中,Anamal.java中的主键生成策略需要注解成@GeneratedValue(strategy=GenerationType.TABLE),测试是在mysql数据库中进行的。
Animal.java
Java代码
- @Entity
- @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
- public class Animal {
- private int id;
- private String name;
- @Id
- @GeneratedValue(strategy=GenerationType.TABLE)
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Cat.java
Java代码
- @Entity
- @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
- public class Cat extends Animal {
- private String catName;
- public String getCatName() {
- return catName;
- }
- public void setCatName(String catName) {
- this.catName = catName;
- }
- }
Dog.java
Java代码
- @Entity
- @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
- public class Dog extends Animal{
- private String dogName;
- public String getDogName() {
- return dogName;
- }
- public void setDogName(String dogName) {
- this.dogName = dogName;
- }
- }
TestJunit.java
Java代码
- @Test
- public void testAnimal(){
- Animal t = new Animal();
- t.setName("animal");
- session.saveOrUpdate(t);
- }
- @Test
- public void testCat(){
- Cat t = new Cat();
- t.setName("cat");
- t.setCatName("cat");
- session.saveOrUpdate(t);
- }
- @Test
- public void testDog(){
- Dog t = new Dog();
- t.setName("dog");
- t.setDogName("dog");
- session.saveOrUpdate(t);
- }
对应生成的数据表是这样的
table---animal
id | name |
1 | animal |
table---cat
id | name | catName |
2 | cat | cat |
table---dog
id | name | dogName |
3 | dog | dog |
转Hibernate继承
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。