一、Hibernate一对一唯一外键关联(单向关联)
(1)实体类与表间的关系
Pepole IDcard tab_people tab_idcard
-id:int id:int id id
-name:string idcard_code:string name IDcard_code
-sex:String sex
-age:int age
-idcard:IDcard card_id
(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,
`card_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `tab_people` (`id`,`name`,`age`,`sex`,`card_id`) VALUES
(1,'小明',22,'男',1),
(2,'小红',20,'女',2),
(3,'小刚',30,'男',3);
(3)、创建实体类与映射文件
IDcard.java
public class IDcard {
private Integer id;// 标识符
private String idcard_code;// 身份证号码
public IDcard(){
}
//省略set、get方法
}
Peopel.java
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>
</class>
</hibernate-mapping>
People3.hbm.xml
<hibernate-mapping>
<class name="com.keli.pojo.People" table="tab_people">
<id name="id">
<generator class="native"/>
</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>
<many-to-one name="idcard" unique="true">
<column name="card_id"/>
</many-to-one>
</class>
</hibernate-mapping>
(4)、测试
public class TestOne2OnetUFK1 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("妖人");
//不能成功保存,因为IDcard是Transient状态
session.save(people);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateInitialize.closeSession();
}
}
public void testSave2(){
Session session = null;
try{
session = HibernateInitialize.getSession();
session.beginTransaction();
IDcard idCard = new IDcard();
idCard.setIdcard_code("888888888888");
session.save(idCard);
People people = new People();
people.setName("容祖儿");
people.setIdcard(idCard);
people.setAge(38);
people.setSex("妖人");
//可以保存
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, 4);
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();
}
}
}
说明:
hibernate一对一唯一外键关联映射(单向关联People---->IDcard)
一对唯一外键关联映射是多对一关联映射的特例
可以采用<many-to-one>标签,指定多的一端的unique=true,这样就限制了多的一端的多重性为一
通过这种手段映射一对一唯一外键关联