Getting Started 08 - Inheritance Map 1
If the object in the application has inherited, we can have three strategies to make this relationship image to the data table. The simplest way is to give each object a table. If you have Field1, Field2 in the parent category, the table user has Field1, Field2, and the subcategory Subuser inherits the Father Class Field1, Field2 properties In the Subuser in the table, there is also the corresponding corresponding to the corresponding, this method is only the convenience of mapping, it is obvious that the parent class and subclasses are allocated, which will become repeated in the database table. And it is difficult to implement multiple operations. It is recommended to use this mapping when you do not need multiple operations. If you write a mapping file for each subcategory, there is nothing special setting. The second way is to store all objects of all inheritance of the same parent category in the same table, and the identification field in the table indicates a list (ROW) belonging to a subcategory or parent class, which is convenient for multiple operations, Moreover, in this topic, we will first explain this method. Let's take a look at the categories and inheritance relationships we write, first is the parent category:
User.java
Package online.
PUBLIC CLASS User {
Private string id;
PRIVATE STRING NAME;
PRIVATE STRING Password;
Public string getId () {
Return ID;
}
Public string getname () {
Return Name;
}
Public string getpassword () {
Return Password;
}
Public void setid (String String) {
ID = String;
}
Public void setname (String string) {
Name = string;
}
Public void setpassword (string password) {
this.password = password;
}
}
Then I will inherit the two subcategories of the User category, the first is the PowerUser category:
PowerUser.java
Package online.
Public Class PowerUser Extends User {
Private int level;
Private string Otherofpower;
Public int getLevel () {
Return Level;
}
Public string getotherofpower () {
Return Other FuPower;
}
Public void setlevel (int level) {
THIS.LEVEL = Level;
}
Public void setotherofpower (String Other FuPower) {
this.otherofpower = otherofpower;
}
}
Below is a GuestUser category that inherits the User category:
GuestUSER.JAVA
Package online.
Public class guestuser extends user {
Private string otherofguest;
Public string getotherofguest () {
Return Otherofguest;
}
Public void setotherofguest (string otherofguest) {this.otherofguest = Otherofguest;
}
}
How to write in the mapping file, because these categories will be imaged in the same table, we use discriminator as each category to record the identification in the table, first look at how the image file is written:
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>
subclass>
subclass>
clas>
hibernate-maping>
In the table, we add a field discriminator_USERTYPE to record the stored category belonging to the user, poweruser, or the guest record, if the field is a ParentUser, indicating that the data is a User category, if it is Power, indicates that is a PowerUser record, If it is guest, it is represented by a GuestUSER record. When the image subcategory is used, the subcategory of the image is used to specify the image of the image and its Discriminator-Value. We can create a data table in the database as follows:
Create Table User
ID Char (32) Not Null,
Discriminator_Usertype varchar (255) Not null,
Name varchar (16) Not null,
Password varchar (16) Not null,
PowerUser_level integer,
Power_other varchar (255),
Guest_other varchar (255),
Primary Key (ID)
);
You can work with the data sheet to create through SchemaExportTask, you can refer to this introduction: use SchemaExportTask
Suppose we store data as follows in the program:
Poweruser PU = New PowerUser ();
Pu.setname ("Caterpillar");
Pu.SetPassword ("123456");
Pu.Setlevel (1);
Pu.setotherofpower ("PowerSer's Field");
GuestUSER GU = New GuestUSER ();
Gu.setname ("momor");
Gu.SetPassword ("654321");
Gu.setotherofguest ("GuestUSER's Field");
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
Session.SAVE (PU);
Session.save (GU);
TX.comMit ();
session.close ();
Then there will be the following content (no ID field):
------------------------ ------------ ----------------------------------- --------
| Discriminator_USERTYPE | NAME | Password | Power_HEVEL | POWER_OTHER | GUEST_OTHER | ----------------------- ----------- - ---------- ------------------------------------------------ - -------------------
| Power | Caterpillar | 123456 | 1 | PowerUser's Field | NULL |
| Guest | MOMOR | 654321 | NULL | NULL | GUESTUSER's Field |
------------------------ ------------ ----------------------------------- --------
You can observe the actual storage method, pay attention to the Discriminator_USERTYPE field, which is used to indicate which type of data belongs to which category is to be queried. If you want to query the data, if you query all Poweruser data, we will do it as follows:
Session session = sessionFactory.openSession ();
List users = session.find ("from poweruser");
session.close ();
For (listiterator itrator = users.listiterator (); item.hasnext ();) {
PowerUser User = (PowerUser) orerator.next ();
System.out.println (user.getname ()
"/ n / tpassword:" user.getpassword ());
System.out.println ("/ TPower:" User.Getotherofpower ()
"/ N / TLEVEL:" User.getlevel ());
}
You can observe the content of Hibernate's true SQL, see where where you know how it queries Poweruser:
{Code: BorderStyle = Solid} SELECT POWERUSER0_.ID AS ID,
PowerUser0_.poweruser_level as poweruse5_,
PowerUser0_.power_other as power_ot6_,
PowerUser0_.name As Name,
PowerUser0_.password as password
From user power 0 0 _ = =;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Use session.find ("from guestuser"); you can query the GuestUSER's data, you can also retrieve data of all USER types, for example:
Session session = sessionFactory.openSession ();
List users = session.find ("from user");
session.close ();
For (listiterator itrator = users.listiterator (); item.hasnext ();) {
User User = (user) iterator.next ();
System.out.println (user.getname ()
"/ n / tpassword:" user.getpassword ());
User InstanceOf Poweruser
System.out.println ("/ TPower:" (PowerUser) User .get mytherofpower ()
"/ N / TLEVEL:" (PowerUser) .Getlevel ());
Else
System.out.println ("/ tguest:" ((guestuser) .Getotherofguest ());
}
Hibernate can use the parent category to get all subcategory data, we know that all Java categories inherit from Object, so if you use session.Find ("from java.LANG.Object"); will retrieve the database All tables of data. Third practices on inheritance relationship maps will be left to the next topic.