Hibernate 多对多关联关系 中间表有多个字段如何配置
2012-02-07 18:39
399 查看
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方式可任意选取一种,以上本人均测试通过。欢迎发邮件到linwu_gao@163.com探讨。
一、需求原因
在我做系统架构时遇到情况是这样:资源菜单实体类(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方式可任意选取一种,以上本人均测试通过。欢迎发邮件到linwu_gao@163.com探讨。
相关文章推荐
- Hibernate 多对多关联关系 中间表有多个字段如何配置
- Hibernate多对多中间表有多个字段字段的注解配置方式(二)
- Hibernate多对多中间表有多个字段字段的注解配置方式(三)
- 如何配置Hibernate 中的表实体类 与 表字段的配置不一样
- hibernate中当Teacher与Student具有多对多的关系时,且使用关联关系,没有实体的中间表,如何使用hql根据student的名字取出拥有他的teacher
- Hibernate多对多中间表有多个字段字段的注解配置方式(一)
- Hibernate使用中间表完成单向多对多关联关系的配置和级联操作示例
- Hibernate多对多中间表有多个字段字段的注解配置方式(二)
- hibernate 表配置文件如何设置表字段的默认值
- Eclipse如何快捷生成Hibernate配置文件
- SSH:Hibernate框架(七种关联关系映射及配置详解)
- hibernate 多对一单向关联关系配置的完整示例
- Hibernate关联关系配置(一对多、一对一和多对多)
- Hibernate关联关系配置(一对多、一对一和多对多)
- 在eclipse中关于Spring和Hibernate 的XML配置如何提示类的包路径的办法
- Spring JDBC和Hibernate混用时,如何配置事务管理
- Hibernate关联关系配置(一对多、一对一和多对多)
- hibernate使用createQuery(hql)语句仅查询部分字段,应如何获得数据
- Hibernate中多对多的annotation的写法(中间表可以有多个额外添加的字段)
- Hibernate关联关系配置(一对多、一对一和多对多)