简体   繁体   English

带有CascadeType.ALL的ManyToMany导致传递的Detched实体持久化

[英]ManyToMany with CascadeType.ALL results in Detached entity passed to persist

I have a many to many relationship with Users and Roles. 我与用户和角色有很多关系。 The user is the owner of the relationship and has CascadeType.ALL. 用户是关系的所有者,并且具有CascadeType.ALL。 When i'm seeding the database, I first create users, create roles and assign the roles to the users. 在为数据库设置种子时,我首先创建用户,创建角色并将角色分配给用户。 Afterwards I save all users with the repository. 之后,我将所有用户保存在存储库中。

I would imagine since I'm saving users, the roles also get saved in the database because of the cascadetype but the error I'm getting is the one mentioned in the title. 我可以想象,由于我在保存用户,因此角色也由于级联类型而保存在数据库中,但是我遇到的错误是标题中提到的错误。

User: 用户:

@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
        name = "users_roles",
        joinColumns = @JoinColumn(
                name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                name = "role_id", referencedColumnName = "id"))
private List<Role> roles;   

Role: 角色:

@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@JsonIgnore
@ManyToMany
private List<User> users;

The code used for seeding the database: 用于播种数据库的代码:

        User user1= new User();
        User user2= new User();

        Role role1 = new Role();
        Role role2 = new Role();

        user1.addRole(role1);
        user2.addRole(role1);
        user2.addRole(role2);

        role1.addUser(user1);
        role1.addUser(user2);
        role2.addUser(user2);

        userRepository.save(user1);
        userRepository.save(user2);

Any help is appreciated! 任何帮助表示赞赏!

EDIT: I found that the problem only occurs when I add multiple roles to the same user and have updated the code. 编辑:我发现仅当我向同一用户添加多个角色并更新了代码时,才会出现问题。

This example works for me, no exception there. 这个例子对我有用,那里也不例外。 Please share more info: hibernate version, database and the actual code that you're running. 请分享更多信息:休眠版本,数据库和您正在运行的实际代码。

Your Cascading here is wrong. 您在这里的级联是错误的。 Having Cascade.ALL on a many to many collection does not make sense exactly because you can have Roles that are shared across users. 在多对多集合上具有Cascade.ALL完全没有意义,因为您可以拥有跨用户共享的角色。

What hibernate is trying to tell you is that you already have a role that has associated ID and the you are trying to invoke persist on the same role. 休眠状态试图告诉您的是,您已经拥有一个具有关联ID的角色,并且您要调用的持久化对象是同一角色。

You need to figure out how to manage your roles. 您需要弄清楚如何管理您的角色。 You have two options: 您有两种选择:

  1. Remove the cascading. 卸下级联。
  2. Reasign the managed roles to your user2, so that you are not using the detached one. 将托管角色重新分配给用户2,这样您就不会使用分离的角色。

Personaly I would remove the Cascade.ALL. 我个人将删除Cascade.ALL。 IMO it is wrong. IMO这是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有 CascadeType.REMOVE 的 CascadeType.ALL 更改持久化行为 - CascadeType.ALL without CascadeType.REMOVE changes persist behavior CascadeType.All不更新关系中的其他实体 - CascadeType.All not updating other entity in a relation org.hibernate.PersistentObjectException:分离的实体传递给持久化-ManyToMany映射 - org.hibernate.PersistentObjectException: detached entity passed to persist - ManyToMany Mapping 通过获取分离实体以保持@ManyToMany双向关系 - Getting detached entity passed to persist with @ManyToMany bidirectional relationship Spring 数据 JPA 和 hibernate 分离的实体传递以保持多对多关系 - Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship 传递给持久化的分离实体 - detached entity passed to persist 分离的实体已传递以保留 - Detached entity passed to persist “分离的实体传递给持久化”当我尝试持久化具有多对多关系的实体时 - “detached entity passed to persist” When i try to persist an Entity with ManyToMany relationship 休眠,多线程和CascadeType.ALL - Hibernate, Multithreading and CascadeType.ALL InvalidDataAccessApiUsageException:分离的实体传递给持久化 - InvalidDataAccessApiUsageException: detached entity passed to persist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM