标签:
Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射。
下面以员工账号和员工档案表为例,介绍这两种映射方式:
1.按照外键映射
步骤一:创建实体类Users1和Resume1
Users1创建如下:
public class Users1 {
private Integer userid;
private String username;
private String userpass;
private Resume1 resume1;
....略
}
Resume1创建如下:
public class Resume1 {
private Integer resid;
private String resname;
private String rescardno;
private Users1 users1;
....略
}
步骤二:配置文件Users1.hbm.xml和Resume1.hbm.xml(小配置)
Users1.hbm.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.onetoone_fk"> <class name="Users1" table="USERS1"> <id name="userid" column="USERID" > <generator class="native"></generator> </id> <property name="username" column="USERNAME" type="string"></property> <property name="userpass" column="USERPASS" type="string"></property> <one-to-one name="resume1" class="Resume1" property-ref="users1"></one-to-one> </class> </hibernate-mapping>
Resume1.hbm.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.onetoone_fk"> <class name="Resume1" table="RESUME1"> <id name="resid" column="RESID" > <generator class="native"></generator> </id> <property name="resname" column="RESNAME" type="string"></property> <property name="rescardno" column="RESCARDNO" type="string"></property> <many-to-one name="users1" class="Users1" cascade="all" column="RESUSERID" unique="true"></many-to-one> </class> </hibernate-mapping>
步骤三:测试方法书写
public void testAdd(){ Session session = HibernateUtil.getSession(); Transaction tx=session.beginTransaction(); //创建一个用户对象 Users1 u1=new Users1("happy","1"); //创建一个档案对象 Resume1 r1=new Resume1("小学档案","happy01"); u1.setResume1(r1); r1.setUsers1(u1); //保存r1自动保存u1 session.save(r1); tx.commit(); System.out.println("ok==="); }
2.按照主键映射
步骤一:创建实体类Users2和Resume2
Users2的创建如下:
public class Users2 {
private Integer userid;
private String username;
private String userpass;
private Resume2 resume2;
}
Resume2的创建如下:
public class Resume2 {
private Integer resid;
private String resname;
private String rescardno;
private Users2 users2;
}
步骤二:配置文件Users1.hbm.xml和Resume1.hbm.xml(小配置)
Users1.hbm.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.onetoone_pk"> <class name="Users2" table="USERS2"> <id name="userid" column="USERID" > <generator class="foreign"> <param name="property">resume2</param> </generator> </id> <property name="username" column="USERNAME" type="string"></property> <property name="userpass" column="USERPASS" type="string"></property> <one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one> </class> </hibernate-mapping>
Resume2.hbm.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.onetoone_pk"> <class name="Resume2" table="RESUME2"> <id column="RESID" name="resid"> <generator class="native"/> </id> <property column="RESNAME" name="resname" type="string"/> <property column="RESCARDNO" name="rescardno" type="string"/> <one-to-one name="users2" cascade="all" class="Users2"/> </class> </hibernate-mapping>
标签:
原文地址:http://www.cnblogs.com/yangronglin/p/5842592.html