角色 与 菜单(资源)的多对多关系,在这里我们建立中间表,用两个oneToMany实现

实体类:

  角色(GmRole)  菜单(GmMenu) 中间表(GmRoleRight)

1.在角色实体中

package com.bossy.gun.entity;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; @Entity
@Table(name="gm_role") public class GmRole implements java.io.Serializable { private static final long serialVersionUID = 2740105939852999578L;
private String roleId;
private String roleName;
private String roleState;
private Set<GmRoleRight> gmRoleRights = new HashSet<GmRoleRight>(0); public GmRole() {
} public GmRole(String roleId) {
this.roleId = roleId;
} public GmRole(String roleId, String roleName, String roleState, Set<GmRoleRight> gmRoleRights) {
this.roleId = roleId;
this.roleName = roleName;
this.roleState = roleState;
this.gmRoleRights = gmRoleRights;
} @Id
@Column(name="role_id", unique=true, nullable=false, length=50)
public String getRoleId() {
return this.roleId;
} public void setRoleId(String roleId) {
this.roleId = roleId;
} @Column(name="role_name", length=100)
public String getRoleName() {
return this.roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} @Column(name="role_state", length=10)
public String getRoleState() {
return this.roleState;
} public void setRoleState(String roleState) {
this.roleState = roleState;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="gmRole")
public Set<GmRoleRight> getGmRoleRights() {
return this.gmRoleRights;
} public void setGmRoleRights(Set<GmRoleRight> gmRoleRights) {
this.gmRoleRights = gmRoleRights;
} @Override
public String toString() {
return "GmRole [roleId=" + roleId + ", roleName=" + roleName
+ ", roleState=" + roleState + ", gmRoleRights=" + gmRoleRights
+ "]";
}
}  

  2.菜单实体中

package com.bossy.gun.entity;

import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; @Entity
@Table(name="gm_menu") public class GmMenu implements java.io.Serializable { private static final long serialVersionUID = -4791621393144183524L;
private String menuId;
private String menuFaId;
private String menuName;
private String menuIoc;
private String menuLink;
private String menuSort;
private String menuState;
private String menuType;
private String menuCkname;
private Set<GmRoleRight> gmRoleRights = new HashSet<GmRoleRight>(0); public GmMenu() {
} public GmMenu(String menuId) {
this.menuId = menuId;
} public GmMenu(String menuId, String menuFaId, String menuName, String menuIoc, String menuLink, String menuSort, String menuState,String menuType,String menuCkname, Set<GmRoleRight> gmRoleRights) {
this.menuId = menuId;
this.menuFaId = menuFaId;
this.menuName = menuName;
this.menuIoc = menuIoc;
this.menuLink = menuLink;
this.menuSort = menuSort;
this.menuState = menuState;
this.gmRoleRights = gmRoleRights;
this.menuType = menuType;
this.menuCkname = menuCkname;
} @Id
@Column(name="menu_id", unique=true, nullable=false, length=50) public String getMenuId() {
return this.menuId;
} public void setMenuId(String menuId) {
this.menuId = menuId;
} @Column(name="menu_fa_id", length=50)
public String getMenuFaId() {
return this.menuFaId;
} public void setMenuFaId(String menuFaId) {
this.menuFaId = menuFaId;
} @Column(name="menu_name", length=100)
public String getMenuName() {
return this.menuName;
} public void setMenuName(String menuName) {
this.menuName = menuName;
} @Column(name="menu_ioc", length=200)
public String getMenuIoc() {
return this.menuIoc;
} public void setMenuIoc(String menuIoc) {
this.menuIoc = menuIoc;
} @Column(name="menu_link", length=400)
public String getMenuLink() {
return this.menuLink;
} public void setMenuLink(String menuLink) {
this.menuLink = menuLink;
} @Column(name="menu_sort", length=10)
public String getMenuSort() {
return this.menuSort;
} public void setMenuSort(String menuSort) {
this.menuSort = menuSort;
} @Column(name="menu_state", length=10)
public String getMenuState() {
return this.menuState;
} public void setMenuState(String menuState) {
this.menuState = menuState;
} @Column(name="menu_type", length=10)
public String getMenuType() {
return menuType;
} public void setMenuType(String menuType) {
this.menuType = menuType;
} @Column(name="menu_ckname", length=10)
public String getMenuCkname() {
return menuCkname;
} public void setMenuCkname(String menuCkname) {
this.menuCkname = menuCkname;
} @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="gmMenu")
public Set<GmRoleRight> getGmRoleRights() {
return gmRoleRights;
}
public void setGmRoleRights(Set<GmRoleRight> gmRoleRights) {
this.gmRoleRights = gmRoleRights;
} }

3.中间表中

package com.bossy.gun.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table; @Entity
@Table(name = "gm_role_right")
public class GmRoleRight implements java.io.Serializable { private static final long serialVersionUID = -1245261414803172237L;
private String grrId;
private String grrState;
private GmRole gmRole;
private GmMenu gmMenu; public GmRoleRight() {
} public GmRoleRight(String grrId) {
this.grrId = grrId;
} public GmRoleRight(String grrId, String grrState, GmRole gmRole,
GmMenu gmMenu) {
super();
this.grrId = grrId;
this.grrState = grrState;
this.gmRole = gmRole;
this.gmMenu = gmMenu;
} @Id
@Column(name = "grr_id", unique = true, nullable = false, length = 50)
public String getGrrId() {
return this.grrId;
}
public void setGrrId(String grrId) {
this.grrId = grrId;
} @Column(name = "grr_state", length = 50)
public String getGrrState() {
return this.grrState;
}
public void setGrrState(String grrState) {
this.grrState = grrState;
} @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="role_id")
public GmRole getGmRole() {
return this.gmRole;
} public void setGmRole(GmRole gmRole) {
this.gmRole = gmRole;
} @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="menu_id")
public GmMenu getGmMenu() {
return gmMenu;
}
public void setGmMenu(GmMenu gmMenu) {
this.gmMenu = gmMenu;
}
@Override
public String toString() {
return "GmRoleRight [grrId=" + grrId + ", grrState=" + grrState
+ ", gmRole=" + gmRole + ", gmMenu=" + gmMenu + "]";
}
}

关于《hibernate多对多》有中间表的建立的更多相关文章

  1. Hibernate多对多关系映射(建表)

    下边讲述Hibernate多对多关系映射. 多对多关系的表的结构为: 两个实体表,还包含一个关系表,关系表为复合主键,如果要使用Hibernate多对多关系映射,则关系表必须只包含两个字段,如果生成了 ...

  2. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  3. hibernate多对一双向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  4. hibernate多对一单向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  5. atitit.atitit.hb many2one relate hibernate 多对一关联配置..

    atitit.atitit.hb many2one relate hibernate 多对一关联配置.. 1. 多对一单向 @ManyToOne 1 1. 其中@JoinColumn 注解 2 2.  ...

  6. hibernate 多对多

    HibernateHibernate多对多关联映射通常别拆分成两个多对一关联映射1. 下面的HostBean.UserBean.UserHostBean,UserHostBean是两个表之间的关联表, ...

  7. hibernate 多对多一个对象出现多条记录问题

    hibernate 多对多时,当须要依据它关联的对象查找的时候,会出现一个对象有多条记录的问题 用 left join fetch 抓取查询的时候还是会出现这问题,是由于主表在关联表中有多条记录 用 ...

  8. Hibernate多对多操作

    ---------------------siwuxie095 Hibernate 多对多操作 以用户和角色为例 (一)多对多映射配置 第一步:创建两个实体类,用户和角色 第二步:让两个实体类之间互相 ...

  9. Hibernate多对多两种情况

    Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...

  10. Hibernate多对多删除问题的解决

    原出处:http://superleo.iteye.com/blog/154587 Hibernate多对多的例子不少,但仔细一看,大多数都是保存的,删除谈的少,但问题还不少,因此有必须简单测试一下, ...

随机推荐

  1. python for mysql

    # -*- coding: utf-8 -*- '''python coded by written in 2016/8/31 Used for get win os log for each win ...

  2. 关于IE11版本下JS中时间判断的问题

    最近在做代码的优化及浏览器的兼容问题.遇到了谷歌.火狐.360兼容模式.IE(8以上)版本对时间判断大小的问题 . 在谷歌.火狐.360.IE11以下IE8以上版本下 var d1="201 ...

  3. Win7下清除SQL SERVER 2008的SSMS保存的登录信息

    C:\Users\{用户名}\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\

  4. Lucene热词显示并选择

    利用Jquery easyui里的autocomplete(1.10.0版本) 的异步请求(remot.html) 添加引用 <script src="~/Scripts/jquery ...

  5. module not exists: xxxx Error in ThinkPHP

    Introduction ThinkPHP is a very charming PHP framework to easily build a website. It's structured in ...

  6. 基于bootstrap + php +ajax datatable 插件的使用

    Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 下面是我学习datatables写的一个服务器端(php)分页例子,该功能包含的功能 ...

  7. Codeforces 747C:Servers(模拟)

    http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...

  8. JavaScript- jquery easyui 可编辑表格插件 easyui.editgrid

    最近项目前端使用 jquery+easyui来做,用了几个月,觉得easyui是一个很不错的开源前端UI框架, 虽然偶尔会遇到一些小问题,但是凭借多年前端开发的实力积累 都一一解决了,其中比较典型的就 ...

  9. java Http编程小结

    1:什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议. HTTP在客户端和服务器之间以reque ...

  10. java Servlet小结

    1:什么是Servlet? ① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③ 这个在服务器端运行,用以处理客户端的请求 2:Servlet 生命周期 Ser ...