"Hibernate at a step"-one-way correlation mapping (i)

Source: Internet
Author: User
Tags generator

The previous article has made a detailed analysis of many-to-one association mappings, it can be implemented in two ways, and the implementation of these two methods is very simple, the key is the use of tags <many-to-one>, it points out the mapping between the multi-and-end, This mapping relationship is both an aggregation relationship in the object model. The next step is to discuss the correlation mappings.

One, the only foreign key

The only foreign key is that the foreign key of each row in the database table corresponds to the primary key in the other table, which means that the primary key of one table is the foreign key of the other table, and the relationship between them is unique, as shown in the relational model:


Of the two entity tables, respectively, for people and identity cards, it is obvious that a person corresponds to an identity card. ID Card as a separate entity table it adds an identity column, and this identity column is first associated with a foreign key in the human entity table. This implementation is similar to the many-to-one mapping described in the previous article, the primary key identity column of a table is a foreign key of another table, where it should be used in the implementation of the <many-to-one> tag, but the difference is that this is the only corresponding, you should set other properties to make a unique identity.

In order to achieve a one-to-one unique foreign key mapping, it is necessary to add the attribute unique in the <many-to-one> tag, that is, the unique value of the specified multi-end is true, the reaction to the example is in the person's mapping file set a < Many-to-one name= "Idcard" > tag, and the tag needs to be added unique= "true", so the corresponding Person.java map file Person.hbm.xml code is as follows:

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2014-5-15 23:47:00 by Hibernate Tools 3.4.0.CR1-->< hibernate-mapping>    <class name= "Com.hibernate.Person" table= "person" >        <id name= "id" type= "int" >            <generator class= "native"/>        </id><property name= "name" ></property>< Many-to-one name= "Idcard" unique= "true" ></many-to-one>    </class>

Comparing many-to-one correlation mappings and single-to-none foreign key association mappings, in fact, they are all two use of <many-to-one> is essentially a foreign key constraint, but one to the other is unique mapping, you need to add Unique= "true" properties, the other two are the same.


second, the primary Key association mapping


The first one-to-one foreign-key mapping is discussed above, which is actually a special case of many-to-a-correlation mappings, and there are a number of situations in the association map where the possible context of the correlation mapping is discussed.
In one-to-one correlation mapping, there is a special case where a table's primary key is also a foreign key to a table, which means that a field is the same as the primary key and foreign key, and that the foreign key uniquely corresponds to a row of another table, they are the only corresponding relationship, reflected in the object model, they belong to a one-to- , the property of one object is the property value of another object, as shown in the object model:

The person class and the Idcard class is a one by one correspondence between the relationship, one can only have one ID card, an identity card can only correspond to one person, and identity card is also a person's identity, it corresponds to the relationship model is a one-to-one relationship, a primary key is also the foreign key of this table, as shown:

The primary key ID column of the T_person table is also the primary key identity column of the T_idcard table, which constitutes a foreign key relationship, so at this point they are a one-to-two primary key association, so how is the reaction to hibernate implemented?
This mapping association provided by hibernate has not only a relationship with <many-to-one>, but also a relationship of <one-to-one>, which refers to a one-to-one relationship.
When implemented, the identity field needs to be set to foreign and the name of the object to be associated as the property parameter is written to the constructor, and the <one-to-one> tag is added at the end. The specific code is as follows:

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2014-5-15 23:47:00 by Hibernate Tools 3.4.0.CR1--><        hibernate-mapping> <class name= "Com.hibernate.Person" table= "person" > <id name= "id" type= "int" > <!--use foreign generation strategy, foreign gets the identity of the associated object--<generator class= "foreign" > <!--prope Rty objects only--<param name= "property" >idCard</param> </generator> </id& Gt;<property name= "name" ></property><!--one-to-one tag indicates how hibernate loads its associated object, which is loaded by default based on the primary key, which is the value of the relationship field. The associated object is loaded based on the primary key of the peer Constrained=true represents the current primary key (the person's primary key) or a foreign key, referencing the primary key of the peer (the primary key of the Idcard), that is, the FOREIGN KEY constraint statement is generated--><one-to-one name = "Idcard" constrained= "true" ></one-to-one> </class>
In the example above, <param name= "property" >idCard</param> Refers to a Idcard attribute in Person.java that is an associated object of the person object, so you need to register to foreign to get the identity of the associated object. In addition, an attribute constrained= "true" is added to the <one-to-one> tag, indicating that the primary key of the person object is also a foreign key. The code in the specific Person.java is as follows:

Package Com.hibernate;public class Person {private Idcard idcard;public Idcard Getidcard () {return idcard;} public void Setidcard (Idcard idcard) {this.idcard = Idcard;} private int id;public int getId () {return ID;} public void setId (int id) {this.id = ID;} private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;}}

The Idcard is a property of the person object, so you need to add an identity column when you associate the mapping with a one-to-many primary key.


Conclusion

The two one-to-one mappings are described above, and are discussed from the object model and the relational model respectively, and it is a special case of a multi-pair mapping for a unique foreign key mapping, just add a unique unique key to <many-to-one> and a unique primary key mapping. This actually requires a <one-to-one> tag, then generates the policy using foreign in the properties of the primary key, and adds the corresponding associated object to the map. There are a number of things that can be discussed in the next article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.