标签:style blog http io ar color os sp for
<?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="com.blackfox.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <!-- 设置在多的一端加入一个外键 --> <set name="students"> <key column="classesid"/> <one-to-many class="com.blackfox.hibernate.Student"/> </set> </class> </hibernate-mapping>
<?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="com.bjpowernode.hibernate.Student" table="t_student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
为什么不让一的一端来维护关系呢?
!!!注意:<key>标签和<many-to-one>标签加入的字段保持一致,否则会产生数据混乱
<?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="com.blackfox.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students" inverse="true"> <key column="classesid"/> <one-to-many class="com.blackfox.hibernate.Student"/> </set> </class> </hibernate-mapping>
<?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="com.blackfox.hibernate.Student" table="t_student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <!--Student也维护了关系 --> <many-to-one name="classes" column="classesid"/> </class> </hibernate-mapping>
!!!注意:采用一对多双向关联的目的主要是解决一对多单向关联的缺陷而不是需求驱动的。
标签:style blog http io ar color os sp for
原文地址:http://blog.csdn.net/mingxuanyun/article/details/41554807