简体   繁体   English

Spring JPA:删除实体时,相关的实体也会被删除

[英]Spring JPA: When I delete an entity, the entities related are deleted too

First of all, thanks for be interested in this question. 首先,感谢您对此问题感兴趣。

The scenario is like that: there is an entity Usuario (user) which has several Role. 场景是这样的:有一个具有多个角色的实体Usuario(用户)。 When I delete an User, all Roles related are deleted too. 删除用户时,所有与之相关的角色也将被删除。

The code for Role is: 角色的代码是:

@Entity
@Table(name = "marte_role")
@XmlRootElement
public class Role implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String nombre;

@ManyToMany(
        fetch = FetchType.EAGER, 
        targetEntity = Usuario.class, 
        cascade = { CascadeType.ALL })
@JoinTable(
        name = "marte_usuario_role", 
        joinColumns = { @JoinColumn(name = "role_id") }, 
        inverseJoinColumns = { @JoinColumn(name = "usuario_id") })
@JsonIgnore
private List<Usuario> users = new ArrayList<Usuario>();

... Getters/setters/builders...

And the code for Usuario is: Usuario的代码是:

@Entity
@Table(name = "marte_usuario")
@XmlRootElement
public class Usuario implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String username;
private String password;
private String email;
private boolean enabled;

@ManyToMany(
        fetch = FetchType.EAGER
        , targetEntity = Role.class
        , cascade = { CascadeType.ALL })
@JoinTable(
        name = "marte_usuario_role"
        , joinColumns = { @JoinColumn(name = "usuario_id") }
        , inverseJoinColumns = { @JoinColumn(name = "role_id") })
private List<Role> roles = new ArrayList<Role>();

@Transient
private int numRoles;

It seems to me that is related with CascadeType.ALL. 在我看来,这与CascadeType.ALL有关。 I've tested with CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE, instead of CascadeType.ALL and then the entity is NOT deleted. 我已经使用CascadeType.PERSIST,CascadeType.REFRESH,CascadeType.MERGE(而不是CascadeType.ALL)进行了测试,然后不删除该实体。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗?

Thanks in advance for your answers. 预先感谢您的回答。

You're not doing anything wrong. 你没做错什么 You specify CascadeType.ALL , which means all operations, including delete, are cascaded to related entities. 您指定CascadeType.ALL ,这意味着所有操作(包括删除)都将级联到相关实体。 If you don't want that to happen, don't use CascadeType.ALL . 如果您不希望这种情况发生,请不要使用CascadeType.ALL

CascadeType.ALL还包括CascadeType.REMOVE ,这就是为什么使用此批注删除实体的原因。

Solved! 解决了!

The answers provided are both correct: remove CascadeType.ALL, but just in the Role entity . 提供的答案都是正确的:删除CascadeType.ALL, 但仅在Role实体中 With this change is possible to remove an Usuario, without deleting all the Role related. 通过此更改,可以删除Usuario,而无需删除所有与Role相关的角色。

@Entity
@Table(name = "marte_role")
@XmlRootElement
public class Role implements Serializable {

    private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String nombre;

@ManyToMany(
        fetch = FetchType.EAGER, 
        targetEntity = Usuario.class
        )
@JoinTable(
        name = "marte_usuario_role", 
        joinColumns = { @JoinColumn(name = "role_id") }, 
        inverseJoinColumns = { @JoinColumn(name = "usuario_id") })
@JsonIgnore
private List<Usuario> users = new ArrayList<Usuario>();
...

Thanks! 谢谢!

暂无
暂无

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

相关问题 当我删除一个实体时,相关的实体被删除并出现错误 - When I delete an entity, the entities related are deleted and get an error JPA,删除子实体时删除实体 - JPA, delete entity when a child entity is deleted 在JPA双向@OnetoMany关系中,当我更新父实体时,数据库中的子实体被删除 - In JPA bidirectional @OnetoMany relationship, when I update the parent entity, the child entities are deleted in database Spring JPA 具有可选返回类型的派生删除返回已删除的实体计数而不是已删除的实体 - Spring JPA Derived Delete with Optional Return Type returns the deleted entity count instead of the deleted entity JPA @Version 尝试删除实体时软删除不起作用 - JPA @Version Soft delete does not work when attempting to a deleted entity 软删除:在Spring Boot JPA Hibernate中删除@OneToMany关系中的父实体后,不会删除子实体 - Soft Delete : Child Entity not being deleted after Deleting parent Entity in @OneToMany relation in Spring Boot JPA Hibernate 在spring boot jpa中一次保存一个实体及其所有相关实体 - Save an entity and all its related entities in a single save in spring boot jpa Spring 数据 JPA 查询相关实体示例 - Spring Data JPA Query By Example with related entities Java Spring JPA存储库实体未删除 - Java spring JPA Repository Entity is not deleted 如何在(多线程)事务性Spring / JPA中删除实体 - How to delete entities in (multithreaded) transactional Spring/JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM