標籤:style class blog c code java
第一步,寫注釋:
<!--xx屬性,本類與Yy(類)的多對一 -->
<!--xx屬性,本類與Yy(類)的一對多 -->
<!--xx屬性,本類與Yy(類)的多對多 -->
<!--xx屬性,本類與Yy(類)的一對一 -->
第二部,拷模版
<!--xx屬性,本類與Yy(類)的多對一 -->
<many-to-one name="" class="" column=""></many-to-one>
<!--xx屬性,本類與Yy(類)的一對多 -->
<set name="">
<key column=""></key>
<one-to-many class=""/>
</set>
<!--xx屬性,本類與Yy(類)的多對多 -->
<set name="" table="">
<key column=""></key>
<many-to-many class="" column=""></many-to-many>
</set>
<!--xx屬性,本類與Yy(類)的一對一 -->
<many-to-one name="" class="" column="" unique="true"></many-to-one>
第三步,填空:
<!--xx屬性,本類與Yy(類)的多對一 -->
<many-to-one name="xx" class="Yy" column=""></many-to-one>
<!--xx屬性,本類與Yy(類)的一對多 -->
<set name="xx">
<key column=""></key>
<one-to-many class="Yy"/>
</set>
<!--xx屬性,本類與Yy(類)的多對多 -->
<set name="xx" table="">
<key column=""></key>
<many-to-many class="Yy" column=""></many-to-many>
</set>
<!--xx屬性,本類與Yy(類)的一對一 -->
<many-to-one name="xx" class="Yy" column="" unique="true"></many-to-one>
說明:
多對一,一對多中的column的名字自己取,但是對應的兩個關係的兩個column必須一樣;
一般在多的一方,把屬性名稱+Id當成column值,一對多 一方也使用這個column,這樣就可以了;
多對多中,tables 是多對多的中間表一般命名中包括兩個表名。 key中的column值得是集合外鍵,指的是引用當前自己表的外鍵,本對象+Id。 many-to-many指的是引用對方的外鍵,一般關聯對象名+Id;
下面我們給出一個例子:
實體類:
Role
/** * 實體:崗位 * @author Jelly * */public class Role implements Serializable{ private Long id;private String name;private Set<User> users = new HashSet<User>();}
hbn.xml
<hibernate-mapping package="com.hqu.oa.domain"><class name="Role" table="hqu_role"><id name="id"><generator class="native" /></id><property name="name" /><!-- users屬性,本類與User多對多關係 --><set name="users" table="hqu_user_role" ><key column="roleId"></key><many-to-many class="User" column="userId"></many-to-many></set></class></hibernate-mapping>
User:
/** * 實體:使用者 * @author Jelly * */public class User implements Serializable{private Long id;private Department department;// 所屬部門private Set<Role> roles = new HashSet<Role>();}User.hbm.xml
<hibernate-mapping package="com.hqu.oa.domain"><class name="User" table="hqu_user"><id name="id"><generator class="native" /></id><!-- department屬性,本類與Department的多對一關聯性 --><many-to-one name="department" class="Department" column="departmentId"></many-to-one><!-- roles屬性,本類與Role的多對多關係 --><set name="roles" table="hqu_user_role" order-by="roleId" lazy="false"><key column="userId"></key><many-to-many class="Role" column="roleId"></many-to-many></set></class></hibernate-mapping>
Department
/** * 實體:部門 * @author Jelly */public class Department implements Serializable{private Long id;private Set<User> users = new HashSet<User>();private Department parent;private Set<Department> children = new HashSet<Department>();}Department.hbm.xml
<hibernate-mapping package="com.hqu.oa.domain"><class name="Department" table="hqu_department"><id name="id"><generator class="native" /></id><!-- users屬性,本類與User的一對多關聯性 --><set name="users"><key column="departmentId"></key><one-to-many class="User" /></set><!-- parnet屬性,本類與Department(上級)的多對一 --><many-to-one name="parent" class="Department" column="parentId"></many-to-one><!-- children屬性,本類與Department(下級)的一對多關聯性 --><set name="children" cascade="delete" order-by="id"><key column="parentId"></key><one-to-many class="Department"/></set></class></hibernate-mapping>