简体   繁体   中英

@Cascade Delete not working (JPA, Hibernate and Spring mvc)

I read many posts and guides about this topic, but I'm still not able to make it work.

I'm not able to delete, from the database, a child Entity that has been just removed from the collection of the parent Entity (of course insert and update operations work on my child collection).

In order to make it easier for you to understand, I created a simple code where, as you can see, I get an object Utente from the database, I remove an object Autorizzazioni from the field autorizzazioniLista and finally I saved the object Utente .

In the pics, you can see that the object Autorizzazioni is removed from the collection.

Here you can see the object Utente taken from the database and what there is inside the collection autorizzazioniLista (there are 2 autorizzazioni : id 8 and id 92). 在此处输入图片说明

Here you can see that an object Autorizzazioni (id 8) is removed from the collection autorizzazioniLista when the object Utente is being saved. 在此处输入图片说明

Here is Utente

@Entity
@Table(name = "utente")
@Component
public class Utente implements Serializable{

    private static final long serialVersionUID = -7124540331184173742L;

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "nome")
    @Size(min = 1, max = 45)
    @Pattern(regexp="^[A-Za-z ']*$")
    @NotBlank
    private String nome;

    @Column(name = "cognome")
    @Size(min = 1, max = 45)
    @Pattern(regexp="^[A-Za-z ']*$")
    private String cognome;

    @Column(name = "email")
    @Email
    @Size(min = 1, max = 70)
    private String email;

    @OneToOne(mappedBy = "utente", cascade = CascadeType.ALL)
    @Valid
    private Autenticazione autenticazione;  

    @OneToMany(mappedBy = "utente", fetch = FetchType.EAGER,  orphanRemoval=true, cascade = CascadeType.ALL)    
    private List<Autorizzazioni> autorizzazioniLista;
}

this is Autorizzazioni :

@Entity
@Table(name = "autorizzazioni")
@Component
public class Autorizzazioni implements Serializable {

    private static final long serialVersionUID = 1167361558860087705L;      

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id; 

    @ManyToOne(targetEntity = Utente.class)
    @JoinColumn(name = "utente", referencedColumnName = "id")
    @Size(min = 1, max = 11)
    private Utente utente;

    @ManyToOne(targetEntity = Autorizzazione.class)
    @JoinColumn(name = "autorizzazione", referencedColumnName = "id")
    @Size(min = 1, max = 11)
    private Autorizzazione autorizzazione;
}

this is Autorizzazione

@Component
@Entity
@Table(name="autorizzazione")
public class Autorizzazione implements Serializable{

    private static final long serialVersionUID = -1118124214231477185L;         

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id", nullable=false, updatable=false)
    private int id; 

    @Size(min = 1, max = 45)
    @NotBlank
    @Pattern(regexp="^[A-Za-z.-_ ]*$")
    @Column(name = "descrizione")
    private String descrizione;
}

Can anybody spot the error?

If you're using the same hibernate Session to load the object AND update the collection by removing an element, you need to dissociate the dependent collection entity from his 'owner' by setting the parent object reference to null. Something along the lines:

Autorizzazioni autorizzazioni = utente.getAutorizzazioniLista().remove(0);
autorizzazioni.setUtente(null);
session.saveOrUpdate(utente);

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