Getting Started 09 - Inheritance Map 2
Continue to the previous topic, let's take a look at the third way of inheriting the relationship image, we give the parent category a table with each subcategory, different from the first method, the form of the parent category image and the table of the subcategory image The same primary key value, the parent class table only records the properties of its own, and if the subcategory is to be queried, the inheritance of the inheritance from the parent category table is permitted through the foreign key reference. It is easy to understand directly, we use the user, PowerUser and GuestUser category in the previous topic, the category is inherited: its image to the database table is as follows: where the primary key value of Power_USER and Guest_USER table will be The primary key value of the USER table is the same, and the power_user_id and guest_user_id are used as a foreign key reference to obtain the Name and Password data of the parent category image. In the mapping file, you want to implement this image, we use the
User.hbm.xml
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
Property>
Property>
joined-subclass>
joined-subclass>
clas>
hibernate-maping>
You can build a database and form yourself, of course, using SchemaExportTask to help you create a table is more convenient. The following is SQL used when SchemaExportTask automatically creates a table:
[SchemaExport] ALTER TABLE GUEST_USER DROP CONSTRAINT FKB62739F25ED19688;
[SchemaExport] ALTER TABLE POWER_USER DROP CONSTRAINT FK38F5D2E586CA74B5;
[SchemaExport] Drop Table if EXISTS User;
[SchemaExport] DROP TABLE IF EXISTS GUEST_USER;
[SchemaExport] DROP TABLE IF EXISTS POWER_USER;
[SchemaExport] Create Table User
[SchemaExport] User_id varchar (255) Not null,
[SchemaExport] Name varchar (16) Not null,
[SchemaExport] Password Varchar (16) Not null,
[SchemaExport] Primary Key (user_id)
[SchemaExport]);
[SchemaExport] Create Table Guest_USER (
[SchemaExport] guest_user_id varchar (255) Not null,
[SchemaExport] Guest_other varchar (255),
[SchemaExport] Primary Key (Guest_USER_ID)
[SchemaExport]);
[SchemaExport] Create Table Power_User
[SchemaExport] Power_User_id varchar (255) Not null,
[SchemaExport] Power_User_level integer,
[SchemaExport] Power_other varchar (255),
[SchemaExport] Primary Key (Power_User_ID)
[SchemaExport]);
[SchemaExport] ALTER TABLE GUEST_USER Add Index FKB62739F25ED19688 (Guest_user_id),
Add constraint fkb62739f25ed19688 Foreign Key (guest_user_id) References user (user_id); [SchemaExport] ALTER TABLE POWER_USER Add Index FK38F5D2E586CA74B5 (Power_user_ID),
Add constraint fk38f5d2e586ca74b5 Foreign key (power_user_id) References user (user_id);
As for the writing of the Java program, you can use the test programs written in the previous topic (you can see, Hibernate will separate the details of the database with the database processing), assume the new test program we use the topic. Increase two data, the result of the database form is as follows:
MySQL> Select * from user;
----------------------------------- ----------
| User_id | Name | Password |
----------------------------------- ----------
| 297E3DBDFF0AF72900FF0AF72D4B0001 | Caterpillar | 123456 |
| 297E3DBDFF0AF72900FF0AF72D4B0002 | MOMOR | 654321 |
----------------------------------- ----------
2 rows in set (0.05 sec)
MySQL> Select * from power_user;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---- -------------------
| Power_user_id | power_user_level | Power_other |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---- -------------------
| 297E3DBDFF0AF72900FF0AF72D4B0001 | 1 | PowerUser's Field |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---- -------------------
1 row in set (0.00 sec)
MySQL> SELECT * from guest_user;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----
| Guest_USER_ID | GUEST_OTHER |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----
297E3DBDFF0AF72900FF0AF72D4B0002 | GuestUSER's Field |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----- 1 ROW IN Set (0.00 SEC)
Learn about the SQL statement used by the storage data to the database helps you understand the underlying mode of operation, the SQL of the new data is as follows:
Hibernate: INSERT INTO USER (Name, Password, User_id) VALUES (?,?,?)
Hibernate: INSERT INTO POWER_USER (POWER_USER_LEVEL, POWER_OTHER, POWER_USER_ID) VALUES (?,?,?)
Hibernate: INSERT INTO USER (Name, Password, User_id) VALUES (?,?,?)
Hibernate: INSERT INTO GUEST_USER (Guest_other, guest_user_id) VALUES (?,?)
If you are interested, you can also look at the SQL statement when querying the data. We can see that I actually use Inner Join to query data. The following is the SQL used by PowerUser:
SELECT POWERUSER0_.POWER_USER_ID AS User_ID,
PowerUser0_.power_user_level as power_us2_1_,
PowerUser0_.power_other as power_ot3_1_,
PowerUser0__1_.name as name0_,
PowerUser0__1_.password as password0_
From power_user power @ u00_
Inner Join User PowerT__1_ on power = PowerUser0__1_.user_id