"Hibernate learning"--orm (i)

Source: Internet
Author: User

Hibernate is a way to achieve ORM the framework. an ORM is an object relational Mapping, a relational mapping of objects. That is, the data of the table in the relational database is mapped to an object, that is, the operation between the table and the table is mapped to the operation between the object and the object, through the entity class to achieve the purpose of the Operation table. In a word, the operation of the database is transformed into the operation of the object, which embodies the object-oriented thought.

One -to-one association mapping policies include primary key associations and unique foreign key associations.

One-way


Primary Key Association

Having two objects have the same primary key value, the table name is a one-to-one relationship between them, and the database has no extra fields to maintain the relationship between them, only by the table's primary key. For example, by maintaining relationships with IDs in two tables , there are no additional fields or tables to maintain their relationships.



It can be seen that since it is a one-way association, Person can hold Idcard objects, and Idcard object does not know Person the existence of. 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 object, by default it is loaded by the primary key, which is the value of the relationship field. The associated object is loaded according to the primary key of the peer constrained= "true" indicates whether the current primary key (the person's primary key) or a foreign key references the primary key of the peer (Idcard's 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 used for many-to-one configurations, but it can also be used to represent an affinity relationship if a unique limit is added. 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, the Person-Side GenerationIdcardfield, byIdcardfield to maintain relationships. Uniquerepresents a unique foreign key association. Therefore, it is still through Personto maintain relationships, so correlation with the primary key only needs to be changed Personthe label in the relationship is<many-to-one>,no need to changeIdcardThe 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: As you can see from the table relationship, the primary Key association is The ID in the person table to maintain the relationship, no extra fields, equivalent to the ID when the primary key and when the foreign key, modify the data is difficult And the only foreign key association is to add a third field to maintain the relationship, and extensibility is clearly associated with the primary key.

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

Two-way One

Primary Key Association

the above is one-way, then set into two-way, that is, idcard also want to have the corresponding person object. Then idcard need to add The person attribute, as follows:


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>

In addition to the one-way association, the bi-directional relationship joins the <one-to-one> tag at the idcard end . 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:

Like 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>


is also in Idcard End Join Association <one -to-one > tags. the name of the relationship field needs to be added as property-ref= "Idcard" . Because idcard does not have a primary key, this property is added.

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

           In all, one-way and two-way differences are the holding of relationships in idcard end join <ONE-TO-ONE> label, This tag does not affect storage and 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, which is person id is the primary key is the foreign key, the latter is to add a relationship field as a foreign key. But all are person

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


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.