One-to-one one-way primary Key Association mappings _ Unidirectional
a pair of primary key correlation mappings--The associated two entities share a primary key (keeping the IDs of two entity objects the same), which avoids unnecessary fields being created.
(One-way Association person---->idcard)
class:
public class Idcard {
Private Integer ID;
Private String Idno;
}
public class Person {
Private Integer ID;
private String name;
Private Idcard Idcard;
}
Hbm.xml
IdCard.hbm.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "Idcard" table= "Idcard1" >
<id name= "id" column= "id" type= "Java.lang.Integer" >
<generator class= "native"/>
</id>
<property name= "Idno" column= "Id_no" type= "java.lang.String"/>
</class>
Person.hbm.xml
<?xml version= "1.0" encoding= "Utf-8"?
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD,
< Class name= "Person" table= "Person1",
<id name= "id" column= "id" Type= "Java.lang.Integer" >
<!--person's primary key source Idcard, which is the primary key of the shared Idcard--
<generator class= "foreign" >
<param name= "Property" >idCard</param>
</generator>
</id>
<property name= "name" column= "name" length= "type=" java.lang.String "/>"
<one-to-one name= "Idcard" ></one-to-one>
<!--the meaning of the one-to-one tag, indicating how hibernate loads its associated object, which is loaded by default based on the primary key,
One-to-one property Cascade=all, that is, when you add a person, you can also add Idcard,
Constrained= "True", indicating that a constraint exists on the current primary key, and the person's primary key is referenced as a foreign key idcard
<one-to-one name= "Idcard" constrained= "true" ></one-to-one>
-
</class>
Application Examples:
public void testSave1 () {
Session session = NULL;
Transaction ta = null;
try{
Session = Hibernateutil.getsession ();
Ta = session.begintransaction ();
Idcard Idcard = new Idcard ();
Idcard.setidno ("88888888888888");
Person p = new person ();
P.setname ("Tom");
P.setidcard (Idcard);
Session.save (Idcard); Can be deleted due to one-to-one default Casedese=all
Session.save (P); Save Idcard, Person2.
Ta.commit ();
}catch (Exception e) {
E.printstacktrace ();
if (ta! = null) {
Ta.rollback ();
}
}finally{
Close session, user becomes detached offline object
Hibernateutil.closesession (session);
}
}
Hibernate:insert into Idcard1 (id_no) VALUES (?)
Hibernate:insert into Person1 (name, id) VALUES (?,?)
public void TestGet1 () {
session session = NULL;
Transaction ta = null;
; try{
session = Hibernateutil.getsession ();
ta = Session.begintransaction ();
Person p = (person) session.get (Person.class, New Integer (2));
System.out.println ("person.name=" + p.getname ());
System.out.println ("idcard.cardno=" + P.getidcard (). Getidno ());
ta.commit ();
}catch (Exception e) {
e.printstacktrace ();
if (ta! = null ) {
ta.rollback ();
}
}finally{
//close session, user becomes detached offline object
Hibernateutil.closesession (session);
}
}
Hibernate:select person0_.id as id1_0_, person0_.name as name1_0_ from Person1 person0_ where person0_.id=?
Person.name=tom
Hibernate:select idcard0_.id as id0_0_, idcard0_.id_no as id2_0_0_ from Idcard1 idcard0_ where idcard0_.id=?
idcard.cardno=88888888888888
===================================================================
War to change to bidirectional, modify the following:
public class Idcard {
Private Integer ID;
Private String Idno;
private person person; Add an object variable
}
IdCard.hbm.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "Idcard" table= "Idcard1" >
<id name= "id" column= "id" type= "Java.lang.Integer" >
<generator class= "native"/>
</id>
<property name= "Idno" column= "Id_no" type= "java.lang.String"/>
<one-to-one name= "Person" ></one-to-one>
</class>
Test Case:
public void TestGet2 () {
Session session = NULL;
Transaction ta = null;
try{
Session = Hibernateutil.getsession ();
Ta = session.begintransaction ();
Idcard IC = (idcard) session.get (Idcard.class, New Integer (1));
System.out.println ("Idcard.idno =" + Ic.getidno ());
System.out.println ("person.name =" + Ic.getperson (). GetName ());
Ta.commit ();
}catch (Exception e) {
E.printstacktrace ();
if (ta! = null) {
Ta.rollback ();
}
}finally{
Close session, user becomes detached offline object
Hibernateutil.closesession (session);
}
}
executes an SQL statement, and is a join because one-to-one defaults to fetch= "join"
Hibernate:select idcard0_.id as id0_1_, idcard0_.id_no as id2_0_1_, person1_.id as id1_0_, person1_.name as name1_0_ from Idcard1 idcard0_ left outer joins Person1 person1_ on Idcard0_.id=person1_.id where idcard0_.id=?
Idcard.idno =88888888888888
Person.name = Tom
If IdCard.hbm.xml <one-to-one name= "person" fetch= "select"></one-to-one>, execute 2 SQL
Hibernate:select idcard0_.id as id0_0_, idcard0_.id_no as id2_0_0_ from Idcard1 idcard0_ where idcard0_.id=?
Hibernate:select person0_.id as id1_0_, person0_.name as name1_0_ from Person1 person0_ where person0_.id=?
Idcard.idno =88888888888888
Person.name = Tom
================================