1.hibernate一对一的关系映射
- 一对一的两种配置
l 共享主键方式
create table tb_person(
id integer primary key ,
name varchar2( 60 ) not null,
birthday date ,
email varchar2( 60 ) not null,
phone varchar2( 10 )
);
create table tb_passport(
id integer
primary key
references tb_person(id) ,
serial varchar2( 10 ) not null ,
expiry integer not null ,
t_date date
);
<class name="Person" table="tb_person">
<id name="id" column="id">
<generator class="increment"></generator>
</id>
<property name="name" type="string" />
<property name="birthday" type="date" />
<property name="email" type="string" />
<property name="phone" type="string" />
<one-to-one name="passport" cascade="all" />
</class>
<class name="Passport" table="tb_passport">
<id name="id" column="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="serial" type="string" />
<property name="expiry" type="integer" />
<property name="date" column="t_date" type="date" />
<one-to-one name="person" constrained="true" cascade="all"></one-to-one>
</class>
l 唯一外键方式
create table tb_company(
id integer primary key ,
name varchar2( 60 ) not null ,
open_date date
);
create table tb_address(
id integer primary key ,
street varchar2( 256 ) not null ,
postcode varchar2( 8 ) not null ,
c_id integer
references tb_company(id) unique
);
<class name="Company" table="tb_company">
<id name="id" column="id">
<generator class="increment"></generator>
</id>
<property name="name" type="string"/>
<property name="openDate" column="open_date" type="date"/>
<one-to-one name="address" property-ref="company" cascade="all"></one-to-one>
</class>
<class name="Address" table="tb_address">
<id name="id" column="id">
<generator class="increment"></generator>
</id>
<property name="street" type="string"></property>
<property name="postcode" type="string"></property>
<many-to-one unique="true" name="company" column="c_id" ></many-to-one>
</class>