jpa 中@ManyToMany 标签使用后,只对关联表进行删除的写法
来源:互联网 发布:照片真假辨别软件 编辑:程序博客网 时间:2024/05/01 23:03
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一样.
0 0
- jpa 中@ManyToMany 标签使用后,只对关联表进行删除的写法
- jpa 中@ManyToMany 标签使用后,只对关联表进行删除的写法
- Hibernate ManyToMany 删除两个实体的关联
- Django 中 ManyToMany 的关联方法
- @OneToMany @ManyToMany 关联表的不同
- JPA中ManyToMany关系问题
- JPA中ManyToMany关系问题
- hibernate manytomany 只删除本表和中间表数值
- Jpa中ManyToMany和OneToMany的双向控制
- hibernate对jpa的支持,自己实现AnnotationSessionFactoryBean,可以对jpa标签进行识别
- jpa manyTOMany
- ManyToMany JPA
- 多对多关联查询 ManyToMany
- JPA之ManyToMany映射的示例
- JPA学习笔记---JPA数据的操作:增加,删除,修改,获取,使用JPQL进行查询
- SpringBoot入门-6(利用jpa连接hibernate,并进行生成表,对表的增加,删除,查询操作)
- Hibernate中使用JPA注解@OneToMany的cascade级联标签
- Hibernate中关于多对多关联表中数据的删除问题
- jpa 数据库映射注解介绍
- Microsoft Macro
- Ajax 获取Map集合
- 使用python+selenium爬取学生信息并入库
- Linux skb->priority Qos
- jpa 中@ManyToMany 标签使用后,只对关联表进行删除的写法
- Android的Window类
- Android自定义Dialog
- 正規表現で文字のチェックを行うサンプル
- [学习]OC-NSString去掉两边的空格,查找字符串出现的位置,获取远程URL 内容
- Clean Code 读书笔记三
- Orchard注意事项
- Firefox不支持event解决方法
- Win10/Win8.1磁盘占用100%的解决方法