一对一的关系:就是一个对象对应着另一个对象,你中有我,我中有你的关系;
例如:用户和身份证一样;
user.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.keith.hibernate.User" table="t_user">
<id name="userid">
<generator class="native" />
</id>
<property name="username" />
<one-to-one name="card" class="cn.keith.hibernate.IdCard" />
</class>
</hibernate-mapping>
idcard.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.keith.hibernate.IdCard" table="t_card">
<id name="cardid">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="cardnum" type="string" />
<one-to-one name="user" class="cn.keith.hibernate.User" />
</class>
</hibernate-mapping>
这样的 关系:
详细见:代码