We all know that the one-to-one ing in hibernate mainly has two policies: (1) one-to-one primary key Association (one-way and two-way ). (2) one-to-one foreign key ing (single and bidirectional ). This article mainly explains the two-way problem in one-to-one foreign key ing. Before that, we will learn about it through an instance.
Person and idcard are one-to-one relationships.
T_person table
ID name idcard (unique)
1 Zhang San
2 Wang Wu 1
Among them, Wang Wu has no idcard, which is also in reality. Some people do not have an ID card.
T_idcard table
Id cardno
1 11111111111111
Entity class:
Idcard
Package com. bjpowernode. hibernate;
Public class idcard {
Private int ID;
Private string cardno;
Private person;
Public Person getperson (){
Return person;
}
Public void setperson (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;
}
}
Person
Package com. bjpowernode. hibernate;
Public class person {
Private int ID;
Private string name;
Private 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;
}
Public idcard getidcard (){
Return idcard;
}
Public void setidcard (idcard ){
This. idcard = idcard;
}
}
(3) configuration file
IdcardOf:
<? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping package = "com. bjpowernode. hibernate">
<Class name = "idcard" table = "t_idcard">
<ID name = "ID">
<Generator class = "native"/>
</ID>
<Property name = "cardno"/>
<One-to-one name = "person" class = "person"/>
</Class>
</Hibernate-mapping>
PersonOf:
<? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping package = "com. bjpowernode. hibernate">
<Class name = "person" table = "t_person">
<ID name = "ID">
<Generator class = "native"/>
</ID>
<Property name = "name"/>
<Role-to-one name = "idcard" cascade = "all" class = "idcard" unique = "true" column = "card_id"/>
</Class>
</Hibernate-mapping>
(3)
Insert data to t_person:
Import org. hibernate. Session;
Import JUnit. Framework. testcase;
Public class one2onetest extends testcase {
Public void testsave1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
Person = new person ();
Person. setname ("Zhang San ");
Session. Save (person );
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
The result is as follows:
Mysql> select * From t_person;
+ ---- + ------ + --------- +
| ID | Name | card_id |
+ ---- + ------ + --------- +
| 1 | Zhang San | null |
+ ---- + ------ + --------- +
1 row in SET (0.00 Sec)
Mysql> select * From t_idcard;
Empty set (0.00 Sec)
Insert data:
Public void testsave2 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
Idcard = new idcard ();
Idcard. setcardno ("1111111111 ");
Session. Save (idcard );
Person = new person ();
Person. setname ("Wang Wu ");
// Establish Association
Person. setidcard (idcard );
Session. Save (person );
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
The database results are as follows:
Mysql> select * From t_person;
+ ---- + ------ + --------- +
| ID | Name | card_id |
+ ---- + ------ + --------- +
| 1 | Zhang San | null |
| 2 | Wang Wu | 1 |
+ ---- + ------ + --------- +
2 rows in SET (0.00 Sec)
Mysql> select * From t_idcard;
+ ---- + ------------ +
| ID | cardno |
+ ---- + ------------ +
| 1 | 1111111111 |
+ ---- + ------------ +
1 row in SET (0.00 Sec)
(4) load the data so that the data can be loaded from the person end to the idcard,
As follows:
Public void testload1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
Person = (person) Session. Load (person. Class, 2 );
System. Out. println ("person. Name =" + person. getname ());
System. Out. println ("person. cardno =" + person. getidcard (). getcardno ());
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
The result is as follows:
Hibernate: Select person0 _. ID as id0_0 _, person0 _. Name asname0_0 _, person0 _. card_id as card3_0_0 _ from t_person person0 _ whereperson0 _. ID =?
Person. Name = Wang Wu
Hibernate: Select idcard0 _. id asid1_1 _, idcard0 _. cardno as cardno1_1 _, person1 _. ID as id0_0 _, person1 _. name asname0_0 _, person1 _. card_id as card3_0_0 _ from t_idcard idcard0 _ left outer joint_person person1 _ on idcard0 _. id = person1 _. ID where idcard0 _. id =?
Person. cardno = 1111111111 and so on, the corresponding idcard of person is found. can I find the person in idcard?
Public void testload2 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
//
Idcard = (idcard) Session. Load (idcard. Class, 1 );
System. Out. println ("idcard. cardno =" + idcard. getcardno ());
System. Out. println ("idcard. Person. Name =" + idcard. getperson (). getname ());
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
The result is as follows:
Hibernate: Select idcard0 _. ID as id1_1 _, idcard0 _. cardno as cardno1_1 _, person1 _. ID as id0_0 _, person1 _. name as name0_0 _, person1 _. card_id ascard3_0_0 _ from t_idcard idcard0 _ left Outer Join t_person person1 _ onidcard0 _. id = person1 _. ID where idcard0 _. id =?
Idcard. cardno = 1111111111
Idcard. Person. Name = James
Is the result correct?
Definitely not. idcard. cardno = 1111111111 This is Wang Wu's idcard. How can I find out Michael? The reason is:
Idcard configuration file problems:
<One-to-one name = "person" class = "person"/>
Change to <one-to-one name = "person" class = "person" property-ref = "idcard"/>
Because: if you do not change it, The idcard will compare its ID with the ID in person (because one-to-one is found by ID). This is not required, because the ID in t_idcard is compared with card_id in t_person, you can use
Property-ref = "idcard": Find the card_id in the t_person table and compare it to find the data we are looking.