"SSH Advanced path" hibernate mapping--one-to-one unidirectional correlation mapping (v)

Source: Internet
Author: User

"SSH Advanced Path" Hibernate fundamentals (i) , the small part introduces the basic principle of hibernate and its core, using the object-oriented thinking operation relational database.

"SSH Advanced path" hibernate build development environment + Simple example (ii) , Xiao-bian set up 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 (c), we introduced the basic mapping of hibernate (that is, the mapping of an entity) related concepts, and give you the implementation of relevant examples, relatively simple.

"SSH Advanced path" hibernate mapping--many-to-one one-way association Mappings (iv), we introduced a many-to-one correlation mapping, and detailed understanding of the mapping technique, we still use this mapping technique to help you understand better.

Let's start today's enjoyable tour.

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 for this example, as shown in:

Object model

It can be seen from:

1, a person only one ID card, the only one identity card number, the object is a one-on 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 the one by one correspondence 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, and the table indicates the relationship between the objects.

2, in fact, it is a special case of a correlation mapping , many of the end plus a unique limit, used to represent a one-to-ones relationship .

So it is mapped with a multiple-to-one label, as follows:

<!--uses <mang-to-one> tags to map, specifying that many of the one end is unique to true, which limits the multiplicity of multiple ends 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:

The other same code is as follows:

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

Because a one-to-one primary key affinity mapping has the following two drawbacks:

1, the flexibility is poor, there is no way to change to a multi-link mapping, can not strain the changeable demand;

2, must first save the association object Idcard, then can maintain person;

Therefore, when mapping one-to-one unidirectional association mappings, we take a unique foreign key association mapping.


In the next blog post, we introduce one-to-one bidirectional association mappings, thanks for attention.

"SSH Advanced path" hibernate mapping--one-to-one unidirectional correlation mapping (v)

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.