歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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

一、需求原因

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

二、設計理念

多對多的關系拆分為兩個一對多

三、具體配置

方式一:XML方式

Role實體

public class Role implements Serializable {

         /*ID*/

         privateLong id;

         /*名稱*/

         privateString name;

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

         PrivateSet<RoleResource> roleResources= new HashSet<RoleResource>();

         //get set

}

Resource實體

public class Resource implements Serializable {

         /*ID*/

         privateLong id;

         /*名稱*/

         privateString name;

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

         privateSet<RoleResource> roleResources = new HashSet<RoleResource>();

         // getset

}

RoleResource輔助實體

public class RoleResource implements Serializable{

         /*ID*/

         privateLong id;

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

         privateRole role;

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

         privateResource resource;

         /*排序字段*/

         privateInteger sort;

         // getset

}

Role.hbm.xml

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

         <classname="Role" table="glw_role">

                   <idname="id" column="id">

                            <generatorclass="native" />

                   </id>

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

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

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

                            <keycolumn="roleId"/>

                            <one-to-manyclass="RoleResource" />

                   </set>

         </class>

</hibernate-mapping>

Resource.hbm.xml

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

         <classname="Resource" table="glw_resource">

                   <idname="id" column="id">

                            <generatorclass="native" />

                   </id>

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

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

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

                            <keycolumn="resourceId"/>

                            <one-to-manyclass="RoleResource"/>

                   </set>

         </class>

</hibernate-mapping>

RoleResource.hbm.xml

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

         <classname="RoleResource" table="glw_role_resource">

                   <idname="id" column="id">

                            <generatorclass="native" />

                   </id>

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

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

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

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

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

         </class>

</hibernate-mapping>

Hibernate.cfg.xml中配置

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

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

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

方式二:Annotation方式

Role實體

@Entity

@Table(name="glw_role")

public class Role {

         @Id

         @GeneratedValue(strategy=GenerationType.TABLE)

         privateLong id;

         @Column(length=50)

         privateString name;

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

         privateSet<RoleResource> roleResources = new HashSet<RoleResource>();

         //get set

}

Resource實體

@Entity

@Table(name="glw_resource")

public class Resource {

         @Id

         @GeneratedValue(strategy=GenerationType.TABLE)

         privateLong id;

         @Column(length=50)

         privateString name;

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

         privateSet<RoleResource> roleResources = new HashSet<RoleResource>();

         // getset

}

RoleResource輔助實體

@Entity

@Table(name="glw_role_resource")

public class RoleResource {

         @Id

         @GeneratedValue(strategy=GenerationType.TABLE)

         privateLong id;

         @Column

         privateInteger sort;

         @ManyToOne(cascade=CascadeType.ALL)

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

         privateRole role;

         @ManyToOne(cascade=CascadeType.ALL)

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

         privateResource resource;

         // getset

}

Hibernate.cfg.xml中配置

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

<mappingclass="com.glw.domain.Resource"/>

<mappingclass="com.glw.domain.RoleResource"/>

四、完畢

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

Copyright © Linux教程網 All Rights Reserved