Hibernate Object Inheritance Methods

Source: Internet
Author: User

Hibernate Object Inheritance Methods

Hibernate Object Inheritance Method

There are three types of hibernate inheritance policies. One is to share one table; the other is to share one table of each class, which stores subclass information and parent information; another method is to connect tables. Each class has a table, but the table corresponding to the subclass only saves its own information. The table corresponding to the parent class stores the information of the parent class, all the information is obtained through association between the subclass table and the parent table.

The first method is to share a table:

@ Entity @ Inheritance (strategy = InheritanceType. SINGLE_TABLE) @ DiscriminatorColumn (name = "discriminator", discriminatorType = DiscriminatorType. STRING) // specifies the field name @ DiscriminatorValue ("person") of the object to be distinguished. // It indicates the object of the object, that is, the public class Parent {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 ;}@ Entity @ DiscriminatorValue ("child1") public class Child1 extends Parent {private String email; public String getEmail () {return title ;} public void setEmail (String email) {this. email = email ;}@ Entity @ DiscriminatorValue ("child2") public class Child2 extends Parent {private String address; public String getAddress () {return score ;} public void setAddress (String address) {this. address = address ;}}

In this case, all the information of the parent class and all sub-classes is stored in the same table, and different classes are differentiated by @ DiscriminatorColumn corresponding to @ DiscriminatorValue. When @ DiscriminatorValue is not specified, the full class name is used as DiscriminatorValue.

The second strategy is to store all the information in one table of each class:

@ Entity @ Inheritance (strategy = InheritanceType. TABLE_PER_CLASS) @ TableGenerator (// a table of a class, the most important thing is to ensure that their IDs are generated by a generator, @ TableGenerator is to control this name = "t_gen ", table = "t_gen_table", pkColumnName = "t_pk", valueColumnName = "t_value", pkColumnValue = "person_pk", initialValue = 1, allocationSize = 1) public class Parent {private int id; private String name; @ Id @ GeneratedValue (generator = "t_gen", strategy = GenerationType. TABLE) // This is generated using the TABLE generator. You can use the same generator to control their IDs without repeating the 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 ;}@ Entity public class Child2 extends Parent {private String address; public String getAddress () {return score;} public void setAddress (String address) {this. address = address ;}@entity public class Child1 extends Parent {private String email; public String getEmail () {return title;} public void setEmail (String email) {this. email = email ;}}

The following three methods are used:

@Entity @Inheritance(strategy=InheritanceType.JOINED)  public class Parent {   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;   }  }  @Entity public class Child2 extends Parent {      private String address;    public String getAddress() {     return score;   }    public void setAddress(String address) {     this.address = address;   }    }   @Entity public class Child1 extends Parent {   private String email;    public String getEmail() {     return title;   }    public void setEmail(String email) {     this.email = email;   }     } 

When table join is used, each class still has its own table, but the table corresponding to the subclass only saves the information of the subclass. The information of its parent class is saved by the table of the parent class. When you need to obtain the complete information of the subclass, connect the table of the subclass to the table of the parent class through table connection to obtain the corresponding information. You can add @ PrimaryKeyJoinColumn (name = "foreignKeyName") to the subclass table to specify the name of the child table relative to the foreign key of the parent table.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.