"Hibernate learning"--orm (i)

Source: Internet
Author: User

Hibernate is a way to achieve ORM the framework.

ORM that Object Relational Mapping . Object Relational Mappings. That is, mapping the data of tables in a relational database into objects. This means that the operation between the table and the table is mapped to the action between the object and the object, through the entity class to achieve the purpose of the action table. In a word, the operation of the database is transformed into the operation of the object, which embodies the object-oriented thought.

a one -to-one association mapping policy contains primary key associations and unique foreign key associations.

One-way


Primary Key Association

Let two objects have the same primary key value, and the table name is a one-to-one relationship between them. The database does not have additional fields to maintain the relationship between them, and is only associated with the primary key of the table. For example, to maintain a relationship by using the ID in two tables . There are no additional fields or tables to maintain the relationship between them.



It can be seen that since it is a one-way association, Person able to hold Idcard object. the idcard object does not know The existence of the person. Therefore, the person side to maintain the relationship.


Person.hbm.xml Relationship Code

<class name= "Com.bjpowernode.hibernate.Person" table= "T_person" ><id name= "id" >   <generator Class = "foreign" >   <!--property only Associates objects--       <param name= "Property" >idcard</param></ Generator></id><property name= "name" ><!--one-to-one indicates how hibernate loads its associated objects. By default, the primary key is loaded to get the relationship field value, and the associated object is loaded according to the primary key of the peer constrained= "true" indicates that the current primary key (person's primary key) or a foreign key is the primary key of the peer (Idcard primary key). That is, a FOREIGN KEY constraint statement is generated--><one-to-one name= "Idcard" constrained= "true" ></class>


IdCard.hbm.xml Code

<class name= "Com.bjpowernode.hibernate.Person" table= "T_idcard" ><id name= "id" >   <generator Class = "Native" ></id>    <property name= "Cardno"/></class>

Unique Foreign Key Association

A foreign key association is taken to be used for many-to-one configurations. But assuming that the only limit is added. can also be used to indicate a one-to-one correlation. Therefore, a unique foreign key association is a special case of many-to-one. is mapped by <many-to-one> .

Table relationships:

           through <MANY-TO-ONE> mapping. will be in person idcard field. Via idcard field to maintain the relationship.

Unique represents a unique foreign key association. Therefore, the relationship is maintained through the person side, so It is only necessary to change the label in the person relationship to be <many-to-one> in relation to the primary key. , you do not need to change the idcard Relationship code.

The main change is Person the mapping of the class:

<class name= "Com.bjpowernode.hibernat.Person" table= "T_person" ><id name= "id" ><generator class= " Native "/></id><property name=" name "/><!--set Idcard as foreign key, uniquely identified as True--><many-to-one name=" Idcard "unique=" true "/></class>

Summary: Through the table relationship can be seen, the primary key association is through The ID in the person table maintains the relationship, there is no extra field, which is equivalent to the ID when the primary key and the foreign key. Changing the data is difficult, and the only foreign key association is to add a third field to maintain the relationship. Extensibility is clearly associated with a primary key.

——————————————————————————————————————————————

Two-way One

Primary Key Association

The above is unidirectional, then set into two-way. that is, Idcard also wants to have the corresponding person object. Then idcard needs to increase the person attribute. For example, the following:


Person relationship mappings are associated with primary key mappings.


Idcard Relational Mappings:

<class name= "Com.bjpowernode.hibernate.Person" table= "T_idcard" ><id name= "id" >   <generator Class = "Native" ></id>    <property name= "Cardno"/><one-to-one name= "person"/></class>

than a one-way association. The relationship between two-way associations increases <one-to-one> tags on the idcard side . Idcard also holds a reference to person. <one-to-one> tags do not affect storage, only affect loading, so the one-way association and the two-linked table structure is the same.

Unique Foreign Key Association

Table relationships:

The same as a one-way association. The resulting table structure is the same.

Person The relationship code is the same one-way.


Idcard Relationship Code:

<class name= "Com.bjpowernode.hibernate.Person" table= "T_idcard" ><id name= "id" >   <generator Class = "Native" ></id>    <property name= "Cardno"/><!--embodies two-way association--><one-to-one name= "Person" property-ref= "Idcard"/></class>


the same is in Idcard -side Increase correlation <one -to-one > tags.

The name of the relationship field needs to be added as property-ref= "Idcard" .

This property is incremented because Idcard is not a primary key.

———————————————————————————————————————————————

          in the comprehensive. One-way and two-way differences are the holding of relationships,in theIdcardEnd Join<one-to-one>label, this tag does not affect storage, only affects loading. So the above four relationships can be thought of as two relationships, primary key association and unique Foreign Key association. The difference between the primary key association and the unique Foreign Key association is the relationship of the primary foreign key. The former is PersoninIDis the primary key and the foreign key. The latter is to add a relationship field as a foreign key. But it's all . Personto maintain relationships.

through the summary, the contrast is particularly clear and easy to understand. The next blog continues the analysis.


"Hibernate learning"--orm (i)

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.