日期:2014-05-16  浏览次数:20478 次

hibernate set集合配置 one-to-many

1.user.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.apache.hibernate.common.db.entity">
	<class name="User" table="T_USER">
		<!--主键由外部程序负责生成 -->
		<id name="id" column="I_ID">
			<generator class="assigned" />
		</id>
		<set name="wife" table="T_WIFE" lazy="true" inverse="true" cascade="all">
			<key column="I_USER_ID" />
			<one-to-many class="org.apache.hibernate.common.db.entity.Wife" />
		</set>
	</class>
</hibernate-mapping>

?2.wife.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.apache.hibernate.common.db.entity">
	<class name="Wife" table="T_WIFE">
		<id name="id" column="I_ID">
			<generator class="assigned" />
		</id>
		<many-to-one name="user" column="I_USER_ID" not-null="true" />
	</class>
</hibernate-mapping>