"SSH Advanced Path" Hibernate fundamentals (i) , the small part introduces the basic principle of hibernate and its core, and adopts the object-oriented thinking operation relational database.
"SSH Advanced path" hibernate build development environment + Simple example (ii) , the small series built the basic Hibernate development environment. and made a simple example, the basic principle of it has a rational understanding.
"SSH Advanced Path" Hibernate basic Mapping (iii), we introduce the concept of Hibernate's basic mapping (that is, mapping an entity). And to give you the implementation of relevant examples. Simpler.
"SSH Advanced path" hibernate mapping--many-to-one one-way association mappings (iv). We introduce many-to-one correlation mappings, and specifically explain the mapping techniques. In this article we still use this mapping technique to help you understand better.
The following is a good day to start today.
Mapping principle
two entity objects are a one-to-two association mappings, that is, an object can only correspond to another unique object .
For example, a person has only one ID card (idcard). Let's take a look at the object model of this example, as seen in the following example:
Object model
It can be seen from:
1, a person only has an ID card, the only one ID number. The object is a one-to-a relationship.
2. People (person) holds a reference to the identity card (Idcard), so that two object relationship maintenance is determined by the personal side.
There are two ways to map from an object model to a relational model: primary key associations and unique foreign key associations, and we continue to look at the following.
Classification:
Primary KEY Association:
1, two entity objects are the same as the primary key to indicate a one by one corresponding relationship between them.
2, do not need extra foreign key field to maintain the relationship, only through the primary key to associate, that person's primary key to rely on Idcard primary key, they share a primary key value.
The two points above are exactly the opposite of the unique foreign Key association.
Relationship model of primary Key Association
Person.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The resulting table structure and test data:
Unique Foreign Key Association:1, two entity objects are associated with a foreign key. Indicates the relationship between objects in a table.
2, in fact, it is a special case of many-to-one association mappings , and a single end with a unique limit, which is used to represent the relationship between a-to.
So it's mapped with many-to-one labels. For example, see the following:
<!--the use of <mang-to-one> tags to map, specifying that many of the one end unique is true, which limits the multiplicity of a multi-end to a, that is, to map.--><many-to-one name= "" unique= "true" ></many-to-one>
The relationship model of the unique foreign key
Person.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The resulting table structure and test data:
Other same code such as the following:
Person
public class Person {private int id;private String name;private idcard idcard;public idcard Getidcard () {return idcard;} public void Setidcard (Idcard idcard) {this.idcard = Idcard;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Idcard
public class Idcard {private int id;private String cardno;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getcardno () {return cardno;} public void Setcardno (String cardno) {this.cardno = Cardno;}}
IdCard.hbm.xml
<?xml version= "1.0"?><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
SuggestionsBecause a one-to-one primary key affinity mapping has the following two drawbacks:
1, the flexibility of poor. There is no way to change to a multi-link mapping, can not strain the changing needs;
2, you must save the associated object Idcard, then talent to maintain the person.
Therefore, when mapping one-to-one unidirectional association mappings, we take a unique foreign key association mapping.
Next blog post. We introduce one-to-one bidirectional correlation mapping, thank you for your attention.
"SSH Advanced path" hibernate mapping--one-to-one unidirectional correlation mapping (v)