一、Hibenate一对一主键关联(双向关联)
(1)实体类与表间的关系
Pepole IDcard tab_people tab_idcard
-id:int id:int id id
-name:string -idcard_code:string name IDcard_code
-sex:String pepole:Pepole sex
-age:int age
-idcard:IDcard
(2)、创建表语句
CREATE TABLE `tab_idcard` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`IDcard_code` varchar(45) NOT NULL COMMENT '身份证号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `tab_idcard` (`id`,`IDcard_code`) VALUES
(1,'22019523321*****'),
(2,'22296325413*****'),
(3,'12245435878*****');
CREATE TABLE `tab_people` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`age` int(10) unsigned NOT NULL,
`sex` varchar(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `tab_people` (`id`,`name`,`age`,`sex``) VALUES
(1,'小明',22,'男'),
(2,'小红',20,'女'),
(3,'小刚',30,'男');
(3)、创建实体类与映射文件
IDcard.java
public class IDcard {
private Integer id;// 标识符
private String idcard_code;// 身份证号码
private People people;
public IDcard(){}
//省略set、get方法
}
public class People {
private Integer id;//唯一性标识
private String name;//公民姓名
private String sex;//公民性别
private String age;//公民年龄
private IDcard1 idcard;//身份证实体对象
public People(){
}
//省略set、get方法
}
IDcard.hbm.xml
<hibernate-mapping>
<class name="com.keli.pojo.IDcard" table="tab_idcard">
<id name="id" column="id" type="int">
<generator class="native"/>
</id>
<property name="idcard_code" type="string" length="45" not-null="true">
<column name="IDcard_code"/>
</property>
<one-to-one name="people"/>
</class>
</hibernate-mapping>
People.hbml.xml
<hibernate-mapping>
<class name="com.keli.pojo.People" table="tab_people">
<id name="id">
<generator class="foreign">
<param name="property">idcard</param>
</generator>
</id>
<property name="name" type="string" length="45">
<column name="name"/>
</property>
<property name="sex" type="string" length="2">
<column name="sex"/>
</property>
<property name="age" type="int">
<column name="age"/>
</property>
<one-to-one name="idcard" constrained="true"/>
</class>
</hibernate-mapping>
(4)、测试
public class TestOne2OnetUK1 extends TestCase{
public void testSave1(){
Session session = null;
try{
session = HibernateInitialize.getSession();
session.beginTransaction();
IDcard idCard = new IDcard();
idCard.setIdcard_code("888888888888");
People people = new People();
people.setName("容祖儿");
people.setIdcard(idCard);
people.setAge(38);
people.setSex("妖人");
//不会出现TransientObjectException异常
//因为一对一主键关系映射中,默认了casecade属性
session.save(people);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateInitialize.closeSession();
}
}
public void testLoad1(){
Session session = null;
try{
session = HibernateInitialize.getSession();
session.beginTransaction();
People p1 = (People)session.load(People.class, 6);
System.out.println("People.name="+p1.getName());
System.out.println("People.age="+p1.getAge());
System.out.println("People.sex="+p1.getSex());
System.out.println("People.idcard_code="+p1.getIdcard().getIdcard_code());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateInitialize.closeSession();
}
}
public void testLoad2(){
Session session = null;
try{
session = HibernateInitialize.getSession();
session.beginTransaction();
IDcard p1 = (IDcard)session.load(IDcard.class, 7);
System.out.println("IDcard.People.name="+p1.getPeople().getName());
System.out.println("IDcard.People.age="+p1.getPeople().getAge());
System.out.println("IDcard.People.sex="+p1.getPeople().getSex());
System.out.println("IDcard.idcard_code="+p1.getIdcard_code());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateInitialize.closeSession();
}
}
}