1:person class
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 class
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;
}
}
2: Configuration file
configuration file for Person class
<class name= "Com.bjsxt.hibernate.Person" table= "T_person" >
<id name= "id" >
<generator class= "native" ></generator>
</id>
<property name= "Name" ></property>
<many-to-one name= "Idcard" unique= "true"/>
</class>
Configuration files for the Idcard class
<class name= "Com.bjsxt.hibernate.IdCard" table= "T_idcard" >
<id name= "id" >
<generator class= "native"/>
</id>
<property name= "Cardno" ></property>
<one-to-one name= "Person" property-ref= "Idcard" cascade= "All"/>
</class>
3: Test Program
public void testSave1 () {
session Session = null;
try{
session = hibernateutils.getsession ();
session.begintransaction ();
idcard idcard = new Idcard ();
idcard.setcardno ("88888888888888");
person person = new Person ();
person.setidcard (Idcard);
person.setname ("AAA");
//cannot be saved successfully because Idcard is the transient state
session.save ( person);
session.gettransaction (). commit ();
}catch (Exception e) {
e.printstacktrace ();
session.gettransaction (). rollback ();
}finally{
hibernateutils.closesession (session);
}
}
//Save the Idcard object first, and then save the person object
public void TestSave2 () {
session Session = null;
try{
session = hibernateutils.getsession ();
session.begintransaction ();
idcard idcard = new Idcard ();
idcard.setcardno ("88888888888888");
session.save (Idcard);
person person = new Person ();
person.setidcard (Idcard);
person.setname ("Zhang Bo");
session.save (person);
session.gettransaction (). commit ();
}catch (Exception e) {
e.printstacktrace ();
session.gettransaction (). rollback ();
}finally{
hibernateutils.closesession (session);
}
}
//Load the person object first, and then load the Idcard object
public void TestLoad1 () {
session Session = null;
try{
session = hibernateutils.getsession ();
session.begintransaction ();
person person = (person) session.load (person.class,5);
system.out.println (Person.getname ());
system.out.println (Person.getidcard (). Getcardno ());
session.gettransaction (). commit ();
}catch (Exception e) {
e.printstacktrace ();
session.gettransaction (). rollback ();
}finally{
hibernateutils.closesession (session);
}
}
Load the Idcard object first, and then load the person object
public void TestLoad2 () {
Session session = NULL;
try{
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Idcard Idcard = (idcard) session.load (idcard.class,2);
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);
}
}
4: summary
Hibernate pair one unique Foreign Key Association mapping (bidirectional Association person<----->idcard)
The
Pair one unique foreign key association bidirectional, need at the other end (Idcard), add <one-to-one> tag,
indicates how hibernate loads its associated object, by default loading the person according to the primary key, foreign Key Association
Mapping, Because two entities have a relationship maintained by the foreign key of the person, you cannot specify a primary key to load
person, but to load according to the foreign key of someone, use the following mapping method:
<one-to-one name= "person" property-ref= "Idcard"/>