标签:
public class Person { private int id; private String name; private IdCard idCard; public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this. idCard = idCard; } public int getId() { return id; } public void setId(int id) { this. id = id; } public String getName() { return name; } public void setName(String name) { this. name = name; } }IdCard类
public class IdCard { private int id; private String name; public int getId() { return id; } public void setId(int id) { this. id = id; } public String getName() { return name; } public void setName(String name) { this. name = name; } }
<hibernate-mapping> <class name ="com.bjpowernode.hibernate.IdCard" table= "t_idCard"> <id name ="id"> <generator class ="native"/> </id > <property name ="cardNo"/> </class > </hibernate-mapping>
<hibernate-mapping> <class name ="com.bjpowernode.hibernate.Person" table= "t_person"> <id name ="id"> <!-- 采用foreign生成策略,foreign会取得关联对象的标识 --> <generator class ="foreign"> <!-- property指关联对象 --> <param name ="property">idCard</ param> </generator > </id > <property name ="name"/> <!-- one-to-one指示hibernate如何加载其关联对象,默认根据主键加载 也就是拿到关系字段值,根据对端的主键来加载关联对象 constrained="true"标识当前主键还是一个外键 参照了对端的主键(IdCard的主键),也就是生成外键约束语句 --> <one-to-one name ="idCard" constrained="true"/> </class > </hibernate-mapping>
<hibernate-mapping> <class name ="com.bjpowernode.hibernate.IdCard" table= "t_idCard"> <id name ="id"> <generator class ="native"/> </id > <property name ="cardNo"/> <one-to-one name ="person" /> </class > </hibernate-mapping>
<hibernate-mapping> <class name ="com.bjpowernode.hibernate.Person" table= "t_person"> <id name ="id"> <generator class ="native"/> </id > <property name ="name"/> <many-to-one name ="idCard" unique="true"/> </class > </hibernate-mapping>
<hibernate-mapping> <class name ="com.bjpowernode.hibernate.IdCard" table= "t_idCard"> <id name ="id"> <generator class ="native"/> </id > <property name ="cardNo"/> <one-to-one name ="person" property-ref="idCard"/> </class > </hibernate-mapping>
标签:
原文地址:http://blog.csdn.net/xdd19910505/article/details/51355192