Hibernate inheritance mapping (simple inheritance, subclass,joined-subclass,union-subclass)

Source: Internet
Author: User
Tags inheritance one table

Inheritance Mappings

Demand:

Parent class: Animal sub-category: Cat Monkey

Simple Inheritance mappings (3 tables, 3 mapping files) (how many subclasses, how many mapping files are written)

Inheritance mapping using subclass (one table, 1 mapping files)

Inheritance mapping using Joined-subclass (3 tables, 1 mapping files)

Using Union-subclass Inheritance Mappings (2 tables, 1 mapping files) recommended


Simple Inheritance Mappings

Simple inheritance mappings, how many subclasses, how many mapping files are written.
Step one: Establish the inheritance relationship of the animal class and Cat class




Step Two: Configure the Cat.hbm.xml mapping file


Step three: Add data to the table to test



After running the console display with the database results:




Step four: Let's try to get the data from the database, respectively, by subclass and parent class.





Inheritance Mappings

Needs: cats, monkeys, animals.

Method 1: Use subclass to map all subclasses to a single table (1 tables)


What's the use of. Subclasses teach much, and subclasses are simpler, that is, there are only individual attributes.

Benefit: Because a mapping file is used, the number of mapping files is reduced. Cons: (not conforming to database design principles) A mapping file: Animal.hbm.xml

Note To add the discriminator discriminator to the zone molecule class

Step one: Establish an inheritance relationship with animal, Cat Monkey class






Step Two: Configure the relevant mapping file (only configure Animal.hbm.xml here)


Step three: Add data to the table and test it.


Run the test a bit ~



Discriminator does not set the class type, default is the full name of the class


Let's specify the type of discriminator.







Method 2: Map each class to the corresponding table (3 tables) by Joined-subclass

A mapping file that stores all subclasses, and the parent classes of subclasses are the corresponding tables;

Disadvantage: The table structure is more complex, inserting a subclass of information, you need 2 sql: Insert to the parent class, insert into the child class. Step one: Establish the animal,cat of the inheritance relationship, Monkey class (IBID., omitted here) Step two: Configure the associated mapping file (same Animal.hbm.xml)
Step three: Establish the test file (IBID., omitted here) then we run the test:




Method 3: (recommended) map each subclass to a table by Union-subclass, parent class does not correspond to table (2 tables) Step One: Establish the animal,cat of the inheritance relationship, Monkey class (IBID., omitted here) Step Two: Configure the associated mapping file (same Animal.hbm.xml)

Step three: Establish the test file (IBID., omitted here) then we run the test:



Because the data operation does not use the animal table, we set it up without generating the animal table








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.