ORM. The full name is (objectrelational Mapping), which is the objects relational mapping. The idea of ORM is to map the data of tables in a relational database into objects. Presented in the form of objects. This enables developers to translate the operations of the database into operations on those objects. Hibernate is the realization of this idea, to facilitate the developers to the object-oriented thinking to achieve the operation of the database.
The main files that Hibernate uses when implementing ORM functionality are mapping classes (*.java), mapping Files (*.hbm.xml), and database configuration files (*.properties/*.cfg.xml). Their respective functions are for example the following.
Mapping Classes ( *.java ): It is a description of the structure of the database table, the table field in the class is described as a property, in the future can be implemented to map the table records into the object of the class.
Mapping file (*.hbm.xml): It is the relationship between the specified database table and the mapping class. Contains the corresponding relationship between the mapping class and the database table, the corresponding relationship of the table field and Class property type, and the corresponding relationship between the table field and the Class property name.
Database configuration file (*.properties/*.cfg.xml): It is the connection information that is required to specify the connection to the database. For example, which database to connect to, username to log in to the database, login password, and connection string.
Of course, you can also put the mapping information of the map class here.
then let's walk in together Hibernate Seven kinds of mapping relationships:
1. One-way, single-link mapping (one-to-one):
A pair of relationships between two objects, for example: Person-idcard (ID)
There are two strategies to implement one-to-one correlation mappings:
* PRIMARY KEY Association: that is, two objects have the same primary key value to indicate a one by one corresponding relationship between them; The database table does not have additional fields to maintain the relationship between them. is only associated with the primary key of the table. For example, with:
Example: one-way primary Key Association sample connection
* Unique FOREIGN KEY association: Foreign Key Association, originally used for many-to-one configuration. But with a unique limit ( using the <many-to-one> tag to map, specify that many end unique is true , which limits the multiplicity of a single end to one ), and can also be used to indicate a to-do relationship. In fact, it is a special case of many-to-one. For example, with:
Example: one-way, single-to-all Foreign Key Association sample connection
Note: Due to one-to-one primary key association mapping extensibility is not good. When our needs change and we want to make them one-to-many, we are unable to manipulate them, so we often use unique foreign key associations to solve this problem when we encounter a single connection, and very little of a primary key association.
2, one-way many-to-one correlation mapping (many-to-one):
Many-to-one correlation mapping principle: Add a foreign key to one end of a multi-side. For example, with:
Key Mapping Code --Add the following tag mappings at one end of the multi-instance:
[Java]view Plaincopy
- <many-to-one name= "Group" column= "GroupID" />
3, unidirectional one-to-many association mappings (one-to-many):
One-to-many correlation mappings and many-to-one correlation mappings are consistent, with the addition of a foreign key at one end to the other. For example with (students and classes):
Note: The difference between it and many-to-one is that the maintenance relationship is different
* Many-to-one maintenance relationship is: a multi-point relationship, with this relationship, loaded in more time can be loaded into the
* One-to-many maintenance relationships are: a point-to-many relationship with this relationship. You can load it up when you load one.
Key Mapping Code --Add the following tag mappings at one end, for example:
[Java]view Plaincopy
- <set name= "Students" >
- <key column="classesid"/>
- <one-to-many class="com.hibernate.Student"/>
- </set>
defect: Due to the multiple endStudentDon't knowClassesthe existence (i.e.StudentNo maintenance andClassesthe relationship) so in the SaveStudentWhen the relationship fieldClassesidis forNULL, assuming that the relationship field is set to non-null. You will not be able to save the data, often the workaround is to use bidirectional correlation mapping, see6.
4, One-way many-to-many mappings (Many-to-many):
Many-to-many association mappings Add a table to complete the basic mapping. For example, with:
Key Mapping Code --can be added at one end of the user such as the following tag mappings:
[Java]view Plaincopy
- < Set name= "roles" table= "T_user_ Role " >
- <key column=< Span class= "string" "user_id" />
- <many-to-many class = "Com.hibernate.Role" column= "role_id" />
- </set>
5. Bidirectional one-to-one correlation mapping :
For one-way mapping, you need to add <one-to-one> tags to the idcard. It does not affect. only affects loading.
For example, with:
Bidirectional one-to-one primary key Mapping key mapping code-- Idcard end new additions such as the following label mappings :
[Java]view Plaincopy
- <one-to-one name= "Person" />
Two-way one-to-one unique foreign key Mapping key mapping code-- Idcard new additions such as the following label mappings :
[Java]view Plaincopy
- <one-to-one name= "Person" property-ref= "Idcard" />
Note: one-to-one unique Foreign key association bidirectional use <one-to-one> label mappings, you must specify <one-to-one> in the label Property-ref property is the name of the relationship field
6, bidirectional one-to-many association mappings ( Very important ):
The purpose of using a one-to-many bi-directional correlation mapping is primarily to solve the defect rather than the demand-driven problem of a pair of multi-directional associations.
A one-to-many bidirectional association mapping method:
* Use <key> tags on one end of the set, add a foreign key at the end of the multiple
* Use <many-to-one> tag at one end of the lot
Note: <key> Labels and <many-to-one> the label added field remains constant. Otherwise, it would create data chaos .
Key Mapping Code:
At one end of the classes is added such as the following label mappings:
[Java]view Plaincopy
- < Set name= "students" inverse= "true" >
- <key column= "Classesid" />
- <one-to-many class = />
- </set>
At one end of the student is added such as the following label mappings:
[Java]view Plaincopy
- <many-to-one name= "Classes" column= "Classesid" />
Gaze: Inverse Properties
* The inverse property can be used on a one-to-many and many-to-many bidirectional association, and the inverse property defaults to False, indicating that the local side can maintain the relationship, assuming inverse is true. Then this side can not maintain the relationship, will be handed over to one end to maintain the relationship, the end of failure. So a one-to-many association map we usually maintain relationships on many of the other side, which invalidates a single end.
* Inverse is the reversal in the control direction, only affects the storage
7, bidirectional Many-to-many association mappings :
The purpose of the two-way is to both sides can be loaded into the other side. And one-way many-to-many differences are two-way need to add tag mapping at both ends, it is important to note that:
* The resulting intermediate table name must be the same
* The fields in the resulting intermediate table must be the same
Role (role) side key mapping code:
[Java]view Plaincopy
- < Set name= "users" table= "T_user_ Role " >
- <key column= "role_id" />
- <many-to-many class = column= " user_id " />
- lt;/set>
User (user)-side key mapping code:
[Java]view Plaincopy
- < Set name= "roles" table= "T_user_ Role " >
- <key Column= "user_id" />
- < Span style= "FONT-FAMILY:SIMSUN; Font-size:14px "> <many-to-many class = column= " role_id " />
- lt;/set>
Summarize
For the above seven kinds of correlation mappings, the most important is a one-to-many mapping, because it is closer to our real life. For example: classrooms and students can be a typical one-to-many relationship. And one of the purposes of our software development is to solve some of the recurring problems in life, to put those repeated questions to the computer to help us finish. So as to improve our efficiency. A word: life away from programming, programming more inseparable from life, learning to combine life, understanding more deeply.
SSH Deep Adventures (i) Hibernate architecture (i)-------mapping analysis--seven mapping relationships