1. 程式人生 > >Hibernate 多對多之拆分中間表增加有多個關係欄位

Hibernate 多對多之拆分中間表增加有多個關係欄位

Hibernate多對多關係中間表有其他屬性的配置方式

一、需求原因

        在我做系統架構時遇到情況是這樣:資源選單實體類(Resource)和角色實體類(Role)是多對多關係,需要各個角色可以個性化自己的資源選單順序。

二、設計理念

多對多的關係拆分為兩個一對多(以下為關係對映圖)


三、具體配置

方式一:XML方式

Role實體

public class Role implements Serializable {

         /*ID*/

         private Long id;

         /*名稱*/

         private String name;

         /*與RoleResource的一對多關係*/

         Private Set<RoleResource> roleResources= new HashSet<RoleResource>();

         //get set

}

Resource實體

public class Resource implements Serializable {

         /*ID*/

         private Long id;

         /*名稱*/

         private String name;

         /*與RoleResource的一對多關係*/

         private Set<RoleResource> roleResources = new HashSet<RoleResource>();

         // getset

}

RoleResource輔助實體

public class RoleResource implements Serializable{

         /*ID*/

         private Long id;

         /*與Role的多對一關係*/

         private Role role;

         /*與Resource的多對一關係*/

         private Resource resource;

         /*排序欄位*/

         private Integer sort;

         // getset

}

Role.hbm.xml

<hibernate-mappingpackage="com.glw.domain">

         <class name="Role" table="glw_role">

                   <id name="id" column="id">

                            <generator class="native" />

                   </id>

                   <property name="name" type="string" not-null="true"unique="true" length="50"/>

                   <!--roleResource,與RoleResource的一對多關係-->

                   <set name="roleResources" order-by="id ASC"inverse="true" lazy="false">

                            <key column="roleId"/>

                            <one-to-many class="RoleResource" />

                   </set>

         </class>

</hibernate-mapping>

Resource.hbm.xml

<hibernate-mappingpackage="com.glw.domain">

         <class name="Resource" table="glw_resource">

                   <id name="id" column="id">

                            <generator class="native" />

                   </id>

                   <property name="name" type="string" not-null="true"length="50"/>

                   <!--roleResources,與RoleResource的一對多關係-->

                   <set name="roleResources" order-by="id ASC"inverse="true" lazy="false">

                            <key column="resourceId"/>

                            <one-to-many class="RoleResource"/>

                   </set>

         </class>

</hibernate-mapping>

RoleResource.hbm.xml

<hibernate-mappingpackage="com.glw.domain">

         <class name="RoleResource" table="glw_role_resource">

                   <id name="id" column="id">

                            <generator class="native" />

                   </id>

                   <property name="sort" type="integer" not-null="true" />

                   <!--role,與Role的多對一關係-->

                   <many-to-one name="role" class="Role" column="roleId" />

                   <!--resource,與Resource的多對一關係-->

                   <many-to-one name="resource" class="Resource"column="resourceId"/>

         </class>

</hibernate-mapping>

Hibernate.cfg.xml中配置

<mapping resource="com/glw/domain/Role.hbm.xml"/>

<mapping resource="com/glw/domain/Resource.hbm.xml" />

<mapping resource="com/glw/domain/RoleResource.hbm.xml" />

方式二:Annotation方式

Role實體

@Entity

@Table(name="glw_role")

public class Role {

         @Id

         @GeneratedValue(strategy=GenerationType.TABLE)

         private Long id;

         @Column(length=50)

         private String name;

         @OneToMany(mappedBy="role",cascade=CascadeType.ALL)

         private Set<RoleResource> roleResources = new HashSet<RoleResource>();

         //get set

}

Resource實體

@Entity

@Table(name="glw_resource")

public class Resource {

         @Id

         @GeneratedValue(strategy=GenerationType.TABLE)

         private Long id;

         @Column(length=50)

         private String name;

         @OneToMany(mappedBy="resource",cascade=CascadeType.ALL)

         private Set<RoleResource> roleResources = new HashSet<RoleResource>();

         // getset

}

RoleResource輔助實體

@Entity

@Table(name="glw_role_resource")

public class RoleResource {

         @Id

         @GeneratedValue(strategy=GenerationType.TABLE)

         private Long id;

         @Column

         private Integer sort;

         @ManyToOne(cascade=CascadeType.ALL)

         @JoinColumn(name="roleId",nullable=true)

         private Role role;

         @ManyToOne(cascade=CascadeType.ALL)

         @JoinColumn(name="resourceId",nullable=true)

         private Resource resource;

         // getset

}

Hibernate.cfg.xml中配置

<mapping class="com.glw.domain.Role"/>

<mapping class="com.glw.domain.Resource"/>

<mapping class="com.glw.domain.RoleResource"/>

四、完畢

Xml和Annotation方式可任意選取一種,以上本人均測試通過。