一对一关联映射

Posted 杨荣林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一对一关联映射相关的知识,希望对你有一定的参考价值。

 

Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射。

 

下面以员工账号和员工档案表为例,介绍这两种映射方式:

1.按照外键映射

     

步骤一:创建实体类Users1Resume1

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.xmlResume1.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.按照主键映射

 

步骤一:创建实体类Users2Resume2

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.xmlResume1.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>

  

 

以上是关于一对一关联映射的主要内容,如果未能解决你的问题,请参考以下文章

mybatis第二天——大纲待更新

hibernate关联映射一对一主键

Hibernate 映射一对一关联关系

Hibernate—— 联合主键 一对一关联关系映射(xml和注解) 和 领域驱动设计

hibernate关联映射

关联映射级联操作关系维护 ---- Hibernate之一对多|多对一关系