jpa 中@ManyToMany 标签使用后,只对关联表进行删除的写法

1. 表单结构 :

  • 一个角色表TRole 
  • 一个菜单表TMenu
  • 一个角色-菜单表TRoleMenu

2.通过jpa生成pojo,用到了many2many 注释来表示关联关系后。只会生成2个实体pojo 类。中间关联表不会生成pojo类

package com.alcor.acl.domain; import java.io.Serializable; import javax.persistence.*; import java.util.Set; /** * The persistent class for the t_menu database table. * */ @Entity @Table(name="t_menu") public class TMenu implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="menu_id") private String menuId; @Column(name="menu_name") private String menuName; private int menuorder; private String module; @Column(name="parent_id") private String parentId; private String url; //bi-directional many-to-many association to TRole @ManyToMany(mappedBy="TMenus", fetch=FetchType.EAGER) private Set<TRole> TRoles; public TMenu() { } public String getMenuId() { return this.menuId; } public void setMenuId(String menuId) { this.menuId = menuId; } public String getMenuName() { return this.menuName; } public void setMenuName(String menuName) { this.menuName = menuName; } public int getMenuorder() { return this.menuorder; } public void setMenuorder(int menuorder) { this.menuorder = menuorder; } public String getModule() { return this.module; } public void setModule(String module) { this.module = module; } public String getParentId() { return this.parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public String getUrl() { return this.url; } public void setUrl(String url) { this.url = url; } public Set<TRole> getTRoles() { return this.TRoles; } public void setTRoles(Set<TRole> TRoles) { this.TRoles = TRoles; } }

package com.alcor.acl.domain; import java.io.Serializable; import javax.persistence.*; import java.util.Set; /** * The persistent class for the t_role database table. * */ @Entity @Table(name="t_role") public class TRole implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="role_id") private String roleId; private String description; @Column(name="role_name") private String roleName; //bi-directional many-to-many association to TMenu @ManyToMany(fetch=FetchType.EAGER) @JoinTable( name="t_role_menu" , joinColumns={ @JoinColumn(name="role_id") } , inverseJoinColumns={ @JoinColumn(name="menu_id") } ) private Set<TMenu> TMenus; //bi-directional many-to-many association to TUser @ManyToMany(mappedBy="TRoles", fetch=FetchType.EAGER) private Set<TUser> TUsers; public TRole() { } public String getRoleId() { return this.roleId; } public void setRoleId(String roleId) { this.roleId = roleId; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public String getRoleName() { return this.roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public Set<TMenu> getTMenus() { return this.TMenus; } public void setTMenus(Set<TMenu> TMenus) { this.TMenus = TMenus; } public Set<TUser> getTUsers() { return this.TUsers; } public void setTUsers(Set<TUser> TUsers) { this.TUsers = TUsers; } }

3.如果需求是只要求解除菜单和角色的关联关系,而不是去删除菜单或者角色。就需要用这样的语法

tRole.getTMenus().remove(tMenu); em.merge(tRole);

注意:只能通过tRole来发起

 

不能写成这样

tMenu.getTRole().remove(tRole); em.merge(tMenu);

这是因为:在pojo的@Many2Many标签只有在TRole这个里面设置了joinColumns属性,指定关系维护端(owner side),即TRole类本身.


使用JoinColumn,name指向数据库字段,referencedColumnName指向实体类里面的属 性,当数据库字段与实体类的名字相同的时候可以省略.


设置inverseJoinColumns,指定关系被维护端(inverse side),即映射到TMenu类,设置方法与joinColumns一样.

你可能感兴趣的:(String,jpa,Module,database,Class,menu)