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