hibernate映射

1,多对一(单向)

<!--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"/>  
<!--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,多对多(单向)

Xml代码 hibernate映射 hibernate映射hibernate映射
  1. <set name="roles" table="t_user_role">  
  2.     <key column="userid"/>  
  3.     <many-to-many class="Role" column="roleid"/>  
  4. </set>  
<set name="roles" table="t_user_role">
	<key column="userid"/>
	<many-to-many class="Role" column="roleid"/>
</set>

 

9,多对多(双向)

Xml代码 hibernate映射 hibernate映射hibernate映射
  1. <set name="users" table="t_user_role" order-by="userid">  
  2.     <key column="roleid"/>  
  3.     <many-to-many class="User" column="userid"/>  
  4. </set>  
  5.   
  6. <set name="roles" table="t_user_role">  
  7.     <key column="userid"/>  
  8.     <many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>  
  9. </set>  
<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 不对查询有影响

相关推荐