Hibernate 多对多关联关系 中间表有多个字段如何配置

Hibernate多对多关系中间表有其他属性的配置方式

一、需求原因

在我做系统架构时遇到情况是这样:资源菜单实体类(Resource)和角色实体类(Role)是多对多关系,需要各个角色可以个性化自己的资源菜单顺序。

二、设计理念

多对多的关系拆分为两个一对多(以下为关系映射图)


三、具体配置

方式一:XML方式

Role实体

public class Role implements Serializable {

         /*ID*/

         private Long id;

         /*名称*/

         private String name;

         /*与RoleResource的一对多关系*/

         Private Set roleResources= new HashSet();

         //get set

}

Resource实体

public class Resource implements Serializable {

         /*ID*/

         private Long id;

         /*名称*/

         private String name;

         /*与RoleResource的一对多关系*/

         private Set roleResources = new HashSet();

         // 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

        

                  

                           

                  

                  

                  

                  

                           

                           

                  

        

Resource.hbm.xml

        

                  

                           

                  

                  

                  

                  

                           

                           

                  

        

RoleResource.hbm.xml

        

                  

                           

                  

                  

                  

                  

                  

                  

        

Hibernate.cfg.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 roleResources = new HashSet();

         //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 roleResources = new HashSet();

         // 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中配置

四、完毕

Xml和Annotation方式可任意选取一种,以上本人均测试通过。欢迎发邮件到[email protected]探讨。


你可能感兴趣的:(hibernate方面)