多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 保存 ~~~ User user = new User(); user.setName("jack"); Role role1 = new Role(); role1.setRole_name("物流管理员"); Role role2 = new Role(); role2.setRole_name("仓库管理员"); user.getRoles().add(role1); user.getRoles().add(role2); role1.getUsers().add(user); role2.getUsers().add(user); entityManager.persist(user); entityManager.persist(role1); entityManager.persist(role2); ~~~ ## 级联保存 User类: ~~~ @ManyToMany(targetEntity = Role.class, cascade = CascadeType.ALL) //设置级联 ~~~ 代码: ~~~ User user = new User(); user.setName("jack"); Role role1 = new Role(); role1.setRole_name("物流管理员"); Role role2 = new Role(); role2.setRole_name("仓库管理员"); user.getRoles().add(role1); user.getRoles().add(role2); entityManager.persist(user); ~~~ ## 删除一个角色 ~~~ User user = entityManager.find(User.class, 15L); Role role = entityManager.find(Role.class, 16L); user.getRoles().remove(role); ~~~ ## 添加一个角色 ~~~ User user = entityManager.find(User.class, 15L); Role role = entityManager.find(Role.class, 16L); user.getRoles().add(role); ~~~ ## 普通删除 就算不配置级联操作,普通删除也会把中间表关联的数据给删除掉. ~~~ Role role = entityManager.find(Role.class, 16L); entityManager.remove(user); ~~~