"SSH Advanced path" hibernate mapping--one-to-one bidirectional correlation mapping (vi)

Source: Internet
Author: User

Previous blog post "SSH Advanced path" hibernate mapping-one-to-one unidirectional correlation mapping (v), we introduced one-to-two unidirectional association mappings, one-way refers to the ability to load the identity card (Idcard) only from the person's side, but in turn. It is not possible to load personal information from the identity card side.

What you see:

The key reason is that the object model has directionality:

unidirectional: One end can only be loaded and one end. cannot be reversed.

bidirectional : Both ends can be loaded and one end.

The question is: how do we want to load people (person) from the ID card (idcard)?

Let's start by introducing a one-to-one bidirectional association mapping.

Mapping principle

bidirectional correlation Mapping and one-way associative mappings the principle is the same, bidirectional associative mapping does not affect storage. only affects loading .

Therefore, the relational model of bidirectional correlation mapping and unidirectional association mapping is the same, that is, the table structure of the database is the same, only the Idcard entity classes and Configuration files (IdCard.hbm.xml) have changed a little.

Object model

It can be seen from:

1, a person only has an identity card, the only one of the identity card, the object of the relationship between the two.

2, two objects have relationship maintenance or a person-side decision ( because the relationship can only be maintained by one end of the primary key, the relationship will be chaotic).

Based on the object model above, we can see that the person side has not changed. But add a reference to the person on the Idcard side, such as the person and Idcard entity classes, for example.

Person

Package Com.liang.hibernate;public class Person {private int id;private String name;private idcard idcard;public idcard ge Tidcard () {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

Package Com.liang.hibernate;public class Idcard {private int id;private String cardno;private person person;public person Getperson () {return person;} public void Setperson (person person) {This.person = person;} 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;}}

Whether they are one-way or two-way associative mappings, they are all part-to-single correlation mappings, only their primary key generation strategy is different. The primary Key association mappings and the unique Foreign Key association mappings.

Since they all belong to one-to-one correlation mappings, hibernate encapsulates bidirectional affinity mappings. The primary key and unique Foreign Key association mapping of the loading strategy, all using a one-to-one <one-to-one name= "" ></one-to-one>, only the property settings are inconsistent, so, the following we separate to see the Idcard configuration file.

Classification:

Primary Key Association Mappings

Similar to a one-way associative mapping, a primary key association is associated with a primary key. The value of the associated primary key is the same. Let's take a look at the mapping file:

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

Person.hbm.xml, same as one-way primary Key Association mappings

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

Unique Foreign Key Association mappings

A one-to-one, foreign-key association mapping of two-way association mappings is similar to the Foreign Key Association mapping of a single-direction association map, where there is a unique foreign key to the point-to-point (person), which is associated with the pointed end (Idcard), and the value of the associated primary key is the same. Let's take a look at the mapping file:

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

Person.hbm.xml, same as one-way unique foreign Key association mappings

<?

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

Control

The difference between one-way and two-way association mappings is one of the differences between the object model and the relational model.

Object model: Directional. Whether it is unidirectional or bidirectional is determined by the object model, which is the configuration file.

Relational model: No directionality or bidirectional. There is one end that can be loaded from either end.

Download

The above content, only proves that one-to-one bidirectional correlation mapping does not affect the storage that does not change the table structure, but can not prove that the association is bidirectional, we need to write the corresponding test example, we are in the form of source code to everyone. Source code Download

Summarize

One-to-two bidirectional correlation mapping is not required and is determined by demand.

Suppose there is no such requirement. The user also did not request. System does not need, there is no need to establish two-way correlation mapping.

Next blog post. We introduce many-to-many one-way association mappings. Thank you for your attention.

"SSH Advanced path" hibernate mapping--one-to-one bidirectional correlation mapping (vi)

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.