【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
hibernate继承映射以下测试是在mysql中进行的。
1、单表方式
Animal.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
- @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
- @Entity
- @DiscriminatorValue(value="dog")
- public class Dog extends Animal{
- private String dogName;
- public String getDogName() {
- return dogName;
- }
- public void setDogName(String dogName) {
- this.dogName = dogName;
- }
- }
TestJunit.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
- @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
- @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
- @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
- @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
- @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
- @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
- @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
- @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 |
来源:oschina
链接:https://my.oschina.net/u/133242/blog/174996