数据关联关系:一对一

要实现Hibernate中一对一的数据关联关系有两种方式:

 1, 主键关联,即两个表共享一个主键

 2,唯一外键关联,即在一个表中记录一个外键字段,参照另一张表的主键

 

一、主键关联

 

1. 首先创建用户表(person) 和身份证表(idcard),二者是一对一的关系

----用户表----
create table Person(
  id varchar2(32) primary key,
  name varchar2(32) not null,
  age number(3) not null
)

-----身份证表-----
create table idcard(
 id varchar2(32) ,
 serial VARCHAR(18) NOT NULL ,    //身份证号码
 expiry number(3),      //使用年限
 foreign key(id) references person(id) on delete cascade
)

 2,创建两个表对应的pojo

  

public class Person {
   
   //主键
   private String id = "";
   
   //用户名
   private String name = "";
   
   //年龄
   private int age = 0;
   
   //身份证对象
   private Idcard idcard = null;
     .............. 

}  

 

public class Idcard {
	
	//主键
	private String id = "";
	
	//号码
	private String serial = "";
	
	//使用年限
	private int expiry = 0;
	
	//用户对象
	private Person Person = null;
                ............
}

 3,分别建立两张表的映射关系

  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" >
<hibernate-mapping package="包名">

    <class name="Person" table="Person">
      <id name="id" column="id" type="string">
         <generator class="uuid.hex"></generator>
      </id>
      <property name="name" column="name" type="string"/>
      <property name="age" column="age" type="int"/>
      <one-to-one name="idcard" class="包名.Idcard" cascade="all" outer-join="true"/>
    </class>
    
</hibernate-mapping>

 

 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" >
<hibernate-mapping package="包名">

    <class name="Idcard" table="Idcard">
     <id name="id" column="id">
        <generator class="foreign">
          <param name="property">person</param>
        </generator>
     </id>
       <property name="serial" column="serial" type="string"/>
       <property name="expiry" column="expiry" type="int"/>
       <one-to-one name="person" class="Person" constrained="true"/>
    </class>
    
</hibernate-mapping>

 

二、唯一外键关联

 

1. 首先创建用户表(person) 和身份证表(idcard),二者是一对一的关系

 

----用户表----
create table person(
  pid varchar2(32) primary key,
  name varchar2(32) not null,
  age number(3) not null
)
----身份证表----
create table idcard(
 id varchar2(32) ,
 serial VARCHAR(18) NOT NULL ,
 expiry number(3),
 pid varchar2(32) ,
 foreign key(id) references person(pid) on delete cascade
)

 

2.创建两张表对应的pojo

public class Person {
   
   //主键
   private String pid = "";
   
   //用户名
   private String name = "";
   
   //年龄
   private int age = 0;
   
   //身份证对象
   private Idcard idcard = null;
     .............. 

}  

 

public class Idcard {
	
	//主键
	private String id = "";
	
	//号码
	private String serial = "";
	
	//使用年限
	private int expiry = 0;
	
	//用户对象
	private Person person = null;
                ............
}

 

 3.分别建立两张表的映射关系

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" >
<hibernate-mapping package="包名">

    <class name="person" table="person">
      <id name="pid" column="pid" type="string">
         <generator class="uuid.hex"/>
      </id>
      <property name="name" column="name" type="string"/>
      <property name="age" column="age" type="int"/>
      <one-to-one name="idcard" class="Idcard" property-ref="person"/>
    </class>
    
</hibernate-mapping>

 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" >
<hibernate-mapping package="包名">

    <class name="Idcard" table="idcard">
      <id name="id" column="id" type="string">
         <generator class="uuid.hex"/>
      </id>
      <property name="serial" column="serial" type="string"/>
       <property name="expiry" column="expiry" type="int"/>
      <many-to-one name="person" class="Person" column="pid"/>
    </class>
    
</hibernate-mapping>

 

你可能感兴趣的:(.net,xml,Hibernate)