Hibernate 的*.hbm.xml文件的填写技巧

  

=================================================================================

模板

<!-- ?属性,本类与?类的多对多的关系 -->

<set name="第一个问号值" table="中间表(类名_对方类名 或 对方类名_类名)其中两个地方的要一样">
<key column=" (本类的名字+Id) "></key>
<many-to-many class="第二个问号值" column="(对方的key里面的column属性的值)"></many-to-many>
</set>

<!-- ?属性,本类与?类的多对一的关系 -->
<many-to-one name="(第一个问号的值)" class="(第二个问号的值)" column="(name的属性名 + Id)"></many-to-one>

<!-- ?属性,本类与?类的一对多 -->
<set name="(第一个问号的值)">
<key column="(与其关联(多对一中)的类的column的值) "></key>
<one-to-many class="(第二个问号的值)" />
</set>


==================================================================================

 

-------------------------------------------------------------------------------------------------------------------

eg:

-------------------------------------------------------------------------------------------------
<!-- department属性,本类与Department的多对一 -->
<many-to-one name="department" class="Department" column="departmentId"></many-to-one>


<!-- roles属性,本类与Role的多对多 -->
<set name="roles" table="itcast_user_role">
<key column="userId"></key>
<many-to-many class="Role" column="roleId"></many-to-many>
</set>


<!-- users属性,本类与User的一对多 -->
<set name="users">
<key column="departmentId"></key>
<one-to-many class="User" />
</set>
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
<!-- parent属性,本类与Department(上级)的多对一 -->
<many-to-one name="parent" class="Department" column="parentId"></many-to-one>

 


<!-- children属性,本类与Department(下级)的一对多 -->
<set name="children">
<key column="parentId"></key>
<one-to-many class="Department" />
</set>
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------


<!-- users属性,本类与User的多对多 -->
<set name="users" table="itcast_user_role">
<key column="roleId"></key>
<many-to-many class="User" column="userId"></many-to-many>
</set>
------------------------------------------------------------------------
------------------------------------------------------------------------

 

你可能感兴趣的:(Hibernate)