1.一对一单向主键关联:
(1)基于主键关联的持久化类不能拥有自己的主键生成策略,必须依附于关联类,由关键类来生成主键。
(2)一对一主键映射中,默认了cascade属性。
Eg:Husband类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 package="com.buaa.hibernate.bean"> <class name="Husband"> <id name="id" column="id"> <generator class="foreign"> <param name="property">wife</param> </generator> </id> <property name="name"></property> <one-to-one name="wife" constrained="true"/> </class> </hibernate-mapping>
Eg:Wife类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 package="com.buaa.hibernate.bean"> <class name="Wife"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping>
2.一对一单向外键关联:
Eg:Husband类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 package="com.buaa.hibernate.bean"> <class name="Husband"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"></property> <many-to-one name="wife" column="wifeId" unique="true"/> </class> </hibernate-mapping>
Eg:Wife类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 package="com.buaa.hibernate.bean"> <class name="Wife"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping>原文链接:https://www.f2er.com/xml/295761.html