hibernate中一对一的关联有两种方式:
主键关联
唯一外键关联
下面以一个人只能有一个身份证,一张身份证只能属于一个人为例,分别说明这两种的关联方式。
1)主键关联方式
主键关联不需要额外的表字段;如果两行是通过这种一对一关系相关联的,那么这两行就共享同样的主关键字值。
Person.hbm.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="org.example.bean.Person" table="TPERSON">
<id name="id" column="PERSON_ID">
<generator class="increment"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>
<one-to-one name="card" class="org.example.bean.PersonCard"
cascade="all" />
</class>
</hibernate-mapping>
对应的类:Person.java
public class Person implements Serializable {
private long id;
private long age;
private String firstname;
private String lastname;
private PersonCard card;
}
PersonCard.hbm.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="org.example.bean.PersonCard" table="TPERSONCARD">
<id name="id" column="ID">
<generator class="increment"/>
</id>
<property name="cardID"/>
<property name="beginDate"/>
<property name="endDate"/>
<one-to-one name="person" class="org.example.bean.Person" constrained="true"/>
</class>
</hibernate-mapping>
对应的类:PersonCard.java
public class PersonCard {
private long id;
private String cardID;
private String beginDate;
private String endDate;
private Person person;
}
2)唯一外键关联
唯一外键关联主要是一个外键和一个惟一关键字对应。
下面是修改后的配置文件和对应的类。
Person.hbm.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="org.example.bean.Person" table="TPERSON">
<id name="id" column="PERSON_ID">
<generator class="increment"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>
<!-- one-to-one
one person to one personcard
-->
<one-to-one name="card" class="org.example.bean.PersonCard"
cascade="all-delete-orphan" property-ref="person" />
</class>
</hibernate-mapping>
对应的类:Person.java
public class Person implements Serializable {
private long id;
private long age;
private String firstname;
private String lastname;
private PersonCard card;
}
PersonCard.hbm.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="org.example.bean.PersonCard" table="TPERSONCARD">
<id name="id" column="ID">
<generator class="increment"/>
</id>
<property name="cardID"/>
<property name="beginDate"/>
<property name="endDate"/>
<!--
one personCard to one person
-->
<many-to-one name="person" class="org.example.bean.Person"
unique="true" not-null="true" >
<column name="PERSON_ID" />
</many-to-one>
</class>
</hibernate-mapping>
对应的类:PersonCard.java
public class PersonCard {
private long id;
private String cardID;
private String beginDate;
private String endDate;
private Person person;
}
posted on 2006-06-19 15:31
jie_java 阅读(242)
评论(0) 编辑 收藏