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.