[SSH advanced]-Hibernate one-to-one (one-to-one)-primary key Association ing

Source: Internet
Author: User

[SSH advanced]-Hibernate one-to-one (one-to-one)-primary key Association ing

In real life, there are many scenarios that require one-to-one ing. For example, each student has only one student ID card and each citizen has only one ID card. Here, we use a citizen ID card for example.

  

One-to-one ing is implemented in Hibernate in two ways: 1. Primary Key Association; 2. Unique foreign key Association. Here we will talk about primary key Association ing.

Primary Key Association ing: The primary key of one table depends on the primary key of another table and establishes a one-to-one relationship. The primary keys of the two associated tables are consistent.

Association ing can be subdivided into one-way Association ing and two-way Association ing.



One-to-one association ing

One-to-one association ing means that an object depends on another object. For example, you can find the ID card of a person:


  
Po object:

IdCard. java

<code class="hljs" cs="">public class IdCard {    private int id;    private String cardNo;      //getter、setter}</code>

Person. java

public class Person {    private int id;    private String name;    private IdCard idCard;    //getter、setter}

Ing file:

IdCard. hbm. xml

<code applescript="" class="hljs">

Person. hbm. xml

<code class="hljs" xml="">



In the above configuration file Is the core of configuring one-to-one association, indicating that a Person corresponds to an IdCard. Constrained = "true" indicates that the primary key of the t_person table also has a foreign key pointing to the primary key of the associated table (t_idCard), which will create constraints on the t_person table, the id of the t_person constraint must be the same as the primary key of the idCard.

Run the program. The actual executed SQL statement is as follows. In addition to creating two tables, the constraints are also created for the t_person table:

alter table t_person drop foreign key FK785BED803EEB3F3Edrop table if exists t_idCarddrop table if exists t_personcreate table t_idCard (id integer not null auto_increment, cardNo varchar(255), primary key (id))create table t_person (id integer not null, name varchar(255), primary key (id))alter table t_person add index FK785BED803EEB3F3E (id), add constraint FK785BED803EEB3F3E foreign key (id) references t_idCard (id)



  Insert Test:

session.beginTransaction();IdCard idCard=new IdCard();idCard.setCardNo(123456789);Person person=new Person();person.setName(danny);person.setIdCard(idCard);session.save(person);session.getTransaction().commit();

In the test, define idCard and person first. after the setIdCard (idCard), the person is saved directly. Although the session does not directly save (idCard), because of the one-to-one primary key Association ing feature, the associated object idCard must be saved first, to save the person. Therefore, when session. save (person) is executed, idCard is saved first.

You can find that the executed SQL statement is:

insert into t_idCard (cardNo) values (?)insert into t_person (name, id) values (?, ?)



  Query Test

Person person = (Person) session. load (Person. class, 1); System. out. println (name of person: + person. getName (); System. out. println (cardNo of person: + person. getIdCard (). getCardNo ());

Actually executed SQL statement:

select person0_.id as id0_0_, person0_.name as name0_0_ from t_person person0_ where person0_.id=?select idcard0_.id as id1_0_, idcard0_.cardNo as cardNo1_0_ from t_idCard idcard0_ where idcard0_.id=?

Execution result

Name: cardNo: 123456789 of dannyperson



One-to-one bidirectional Association ing

One-to-one bidirectional Association ing means that two objects depend on each other. Based on a person's ID card, the person's ID card can also be found:

  

In the previous one-to-one association ing, IdCard can be found based on Person, but the Person cannot be queried only based on IdCard. The Person can also be queried Based on IdCard, the po and configuration file of IdCard can also be written as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPqGhoaFJZENhcmQuamF2YTwvcD4NCjxwcmUgY2xhc3M9 "brush: java;"> public class IdCard { private int id; private String cardNo; private Person person;}

IdCard. hbm. xml

<code class="hljs" xml="">

The reason why constrained = "true" cannot be added here is: if the t_person primary key is added, the t_idCard primary key depends on the t_idCard primary key, and the t_idCard primary key also depends on the t_person primary key, is it an "endless loop?

In this way, when querying IdCard, you can find the person

IdCard idCard = (IdCard) session. load (IdCard. class, 1); System. out. println (cardNo: + idCard of person. getCardNo (); System. out. println (name of person: + idCard. getPerson (). getName ());

Execution result:

Person's cardNo: 123456789person's name: danny

Query the executed SQL statement:

select idcard0_.id as id1_0_, idcard0_.cardNo as cardNo1_0_ from t_idCard idcard0_ where idcard0_.id=?Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_ from t_person person0_ where person0_.id=?

However, tags only affect queries and do not affect storage. For example, when you save the following:

session.beginTransaction();Person person=new Person();person.setName(danny);IdCard idCard=new IdCard();idCard.setCardNo(123456789);          idCard.setPerson(person);session.getTransaction().commit();

Only idCard is saved, but person is not saved.

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.