如何通过删除父对象从DATABASE中删除一个集合?

时间:2017-03-23 10:11:45

标签: jpa sql-delete cascade

我有一个包含集合的对象。我想在删除父对象后删除此集合中的所有对象。

这是我的模型Ressource

@Entity
public class Ressource implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long idt_ressource;
    private String nom;
    private String prenom;
    private String telephone;
    private String matricule;
    private String mail;
    private Date dateEntree;
    @OneToMany(mappedBy="ressource")
    private Collection<Affectation> affectations;

    // Getters, Setters and Construct
}

这是我的模特Affectation

@Entity
public class Affectation implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long idt_affectation;
    private Date dateDebut;
    private Date dateFin;
    @ManyToOne
    @JoinColumn(name="idt_ressource")
    private Ressource ressource;
    @ManyToOne
    @JoinColumn(name="idt_structure")
    private Structure structure;

    // Getters, Setters and Construct 
}

1 个答案:

答案 0 :(得分:2)

   @OneToMany(cascade = CascadeType.ALL,mappedBy="ressource")
   private Collection<Affectation> affectations;