【问题标题】:@Cascade Delete not working (JPA, Hibernate and Spring mvc)@Cascade Delete 不起作用(JPA、Hibernate 和 Spring mvc)
【发布时间】:2016-06-12 17:11:23
【问题描述】:

我阅读了很多关于这个主题的帖子和指南,但我仍然无法让它发挥作用。

我无法从数据库中删除刚刚从父实体的集合中删除的子实体(当然插入和更新操作适用于我的子集合)。

为了让您更容易理解,我创建了一个简单的代码,如您所见,我从数据库中获取了一个对象 Utente,然后删除了一个对象 Autorizzazioni 从字段 autorizzazioniLista 最后我保存了对象 Utente

在图片中,您可以看到对象 Autorizzazioni 已从集合中移除。

在这里您可以看到从数据库中获取的对象 Utente 以及集合 autorizzazioniLista 中的内容(有 2 个 autorizzazioni:id 8 和 id 92)。

在这里您可以看到在保存对象 Utente 时,从集合 autorizzazioniLista 中删除了一个对象 Autorizzazioni(id 8)。

这里是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;
}

这是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;
}

这是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;
}

谁能发现错误?

【问题讨论】:

    标签: java hibernate spring-mvc jpa cascade


    【解决方案1】:

    如果您使用相同的休眠Session 加载对象并通过删除元素来更新集合,则需要通过将父对象引用设置为 null 来将依赖集合实体与其“所有者”分离。类似的东西:

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

    【讨论】:

    • 感谢您的帮助。其实我已经尝试过了,但它不起作用。它只是将数据库相对记录上的字段 utente 设置为空。 Autorizzazioni 的相关记录不会从数据库中删除。
    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多