Hibernate多对多关系中间表有其他属性的配置办法
发布时间:2021-11-24 14:29:09 所属栏目:教程 来源:互联网
导读:一、需求原因 在我做系统架构时遇到情况是这样:资源菜单实体类(Resource)和角色实体类(Role)是多对多关系,需要各个角色可以个性化自己的资源菜单顺序。 二、设计理念 多对多的关系拆分为两个一对多 三、具体配置 方式一:XML方式 Role实体 public class Ro
一、需求原因 在我做系统架构时遇到情况是这样:资源菜单实体类(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方式可任意选取一种,以上本人均测试通过。 (编辑:我爱故事小小网_铜陵站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |