简体   繁体   中英

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. 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:

@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. I've tested with CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE, instead of CascadeType.ALL and then the entity is NOT deleted.

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. If you don't want that to happen, don't use CascadeType.ALL .

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

Solved!

The answers provided are both correct: remove CascadeType.ALL, but just in the Role entity . With this change is possible to remove an Usuario, without deleting all the Role related.

@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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM