hibernate映射
程序员文章站
2022-03-02 13:50:07
...
1,多对一(单向)
<!--Person端--> <many-to-one name="group" column="groupid" cascade="all,none,save-update,delete"/>
2,一对一(主键,单向)默认了cascade
<!--Person端--> <!--该段cascade="all"--> <id name="id"> <generator class="foreign"> <param name="property">idCard</param> </generator> </id> <one-to-one name="idCard" constrained="true"/> <!--constrained属性使数据库添加约束-->
3,一对一(主键,双向)
<!--Person端--> <!--该段cascade="all"--> <id name="id"> <generator class="foreign"> <param name="property">idCard</param> </generator> </id> <one-to-one name="idCard" constrained="true"/> <!--IdCard端--> <!--该端不维护关系(inverse="false")--> <one-to-one name="person"/> <!--默认是主键,所有不用加property-ref属性-->
4,一对一(外键,单向)
<!--Person端--> <many-to-one name="idCard" unique="true"/>
5,一对一(外键,双向)
<!--Person端--> <many-to-one name="idCard" unique="true"/> <!--IdCard端--> <!--该端不维护关系(inverse="false")--> <one-to-one name="person" property-ref="idCard"/>
6,一对多(单向)
<!--Class端--> <set name="students"> <key column="classesid"/> <one-to-many class="Student"/> </set>
7,一对多(双向)
<!--Class端-->
<set name="students" inverse="true"><!-- inverse="true" 多的端不维护关系-->
<key column="classesid"/>
<one-to-many class="Student"/>
</set>
<!--Student端-->
<many-to-one name="classes" column="classesid"/>
8,多对多(单向)
<set name="roles" table="t_user_role"> <key column="userid"/> <many-to-many class="Role" column="roleid"/> </set>
9,多对多(双向)
<set name="users" table="t_user_role" order-by="userid"> <key column="roleid"/> <many-to-many class="User" column="userid"/> </set> <set name="roles" table="t_user_role"> <key column="userid"/> <many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/> </set>
10,总结
10.1:<many-to-one> 会在表中添加字段的
10.2:cascade 不对查询有影响