one-to-one 主键关联
数据库脚本:
-- 删除表
DROP TABLE idcard ;
DROP TABLE person ;
-- 创建表
CREATE TABLE person
(
id
VARCHAR(32) NOT NULL PRIMARY KEY ,
name
VARCHAR(20) NOT NULL ,
age
int
) ;
CREATE TABLE idcard
(
id
VARCHAR(32) ,
serial
VARCHAR(18) NOT NULL ,
expiry
int ,
FOREIGN
KEY(id) REFERENCES person(id) ON DELETE CASCADE
) ;
Person表的hbm配置:
<hibernate-mapping>
<class
name="org.lxh.hibernate.demo04.pojo.Person"
table="person">
<id
name="id" type="java.lang.String">
<column
name="id" length="32" />
<generator
class="uuid.hex"></generator>
</id>
<property
name="name" type="java.lang.String">
<column
name="name" length="20" not-null="true" />
</property>
<property
name="age" type="java.lang.Integer">
<column
name="age" />
</property>
<one-to-one
name="idCard"
class="org.lxh.hibernate.demo04.pojo.IDCard"
cascade="all"
outer-join="true">
</one-to-one>
</class>
</hibernate-mapping>
IDCard表的hbm配置:
<hibernate-mapping>
<class
name="org.lxh.hibernate.demo04.pojo.IDCard"
table="idcard">
<id
name="id" column="id" type="java.lang.String">
<generator
class="foreign">
<param
name="property">person</param>
</generator>
</id>
<one-to-one
name="person"
class="org.lxh.hibernate.demo04.pojo.Person"
constrained="true">
</one-to-one>
<property
name="serial" type="java.lang.String">
<column
name="serial" length="18" not-null="true" />
</property>
<property
name="expiry" type="java.lang.Integer">
<column
name="expiry" not-null="true" />
</property>
</class>
</hibernate-mapping>
à唯一外键关联
此例子为mang–to-one
-- 删除表
DROP TABLE person ;
DROP TABLE tgroup ;
-- 创建表
CREATE TABLE person
(
id
VARCHAR(32) PRIMARY KEY NOT NULL ,
name
VARCHAR(20) NOT NULL ,
age
INT ,
gid
VARCHAR(32)
) ;
CREATE TABLE TGROUP
(
gid
VARCHAR(32) PRIMARY KEY NOT NULL,
name
VARCHAR(20)
) ;
commit ;
àPerson.hbm.xml
<hibernate-mapping>
<class
name="org.lxh.hibernate.demo05.pojo.Person"
table="person">
<id
name="id" type="java.lang.String">
<column
name="id" length="32" />
<generator
class="uuid.hex"></generator>
</id>
<property
name="name" type="java.lang.String">
<column
name="name" length="20" not-null="true" />
</property>
<property
name="age" type="java.lang.Integer">
<column
name="age" />
</property>
<many-to-one
name="tgroup"
class="org.lxh.hibernate.demo05.pojo.TGroup"
column="gid"
unique="true">
</many-to-one>
</class>
</hibernate-mapping>
àTGroup.hbm.xml
<hibernate-mapping>
<class
name="org.lxh.hibernate.demo05.pojo.TGroup"
table="tgroup">
<id
name="gid" type="java.lang.String">
<column
name="gid" length="32" />
<generator
class="uuid.hex"></generator>
</id>
<property
name="name" type="java.lang.String">
<column
name="name" length="20" not-null="true" />
</property>
<one-to-one
name="person"
class="org.lxh.hibernate.demo05.pojo.Person"
property-ref="tgroup">
</one-to-one>
</class>
</hibernate-mapping>
àone-to-many
àPerson.hbm.xml
<hibernate-mapping>
<class
name="org.lxh.hibernate.demo06.pojo.Person" table="person"
catalog="mldn">
<id
name="pid" type="java.lang.String">
<column
name="pid" length="32" />
<generator
class="uuid.hex"></generator>
</id>
<property
name="name" type="java.lang.String">
<column
name="name" length="20" not-null="true" />
</property>
<property
name="age" type="java.lang.Integer">
<column
name="age" />
</property>
<set
name="address" table="address" cascade="all"
inverse="true">
<key>
<column
name="pid" length="32" />
</key>
<one-to-many
class="org.lxh.hibernate.demo06.pojo.Address" />
</set>
</class>
</hibernate-mapping>
àAddress.hbm.xml
<hibernate-mapping>
<class name="org.lxh.hibernate.demo06.pojo.Address"
table="address" catalog="mldn">
<id name="aid" type="java.lang.String">
<column name="aid" length="32" />
<generator class="uuid.hex"></generator>
</id>
<many-to-one name="person"
class="org.lxh.hibernate.demo06.pojo.Person">
<column name="pid" length="32" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50"
not-null="true" />
</property>
<property name="zipcode"
type="java.lang.String">
<column name="zipcode" length="10"
not-null="true" />
</property>
</class>
</hibernate-mapping>
-- 学海无涯