You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Pawel Veselov <pa...@gmail.com> on 2015/07/15 20:34:18 UTC

Auto-deleting referenced entities.

Hi.

Is it possible in JPA to reference entities from another, and have them
automatically delete if the reference is just removed?

I.e. if the entity is:

public class E_Campaign implements Accounted {

    @Id
    private long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "campaign")
    private Collection<E_VehicleAssociation> vehicles;

}

And the dependent entity is:

public class E_VehicleAssociation {

    @Id
    private long id;

    @ManyToOne
    private E_Campaign campaign;

}

Is it possible to do something like this, and have that delete the
dependent entity:

campaign.getVehicles().remove(0);
em.merge(campaign);

I.e., I'm removing the reference to an element of campaign.vehicles() would
remove that entity from the VehicleAssociation table? More specifically,
I'm interested in auto-deleting all entities that are references by a
particular campaign, and want to avoid iterating and deleting.

Thank you!
  Pawel.

P.S. Any idea when JPA2.1 will be implemented by OpenJPA? :)

RE: Auto-deleting referenced entities.

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
Yes it is. Simply add "orphanRemoval = true" to the @OneToMany annotation.

Henno

-----Oorspronkelijk bericht-----
Van: Pawel Veselov [mailto:pawel.veselov@gmail.com] 
Verzonden: woensdag 15 juli 2015 20:34
Aan: users@openjpa.apache.org
Onderwerp: Auto-deleting referenced entities.

Hi.

Is it possible in JPA to reference entities from another, and have them automatically delete if the reference is just removed?

I.e. if the entity is:

public class E_Campaign implements Accounted {

    @Id
    private long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "campaign")
    private Collection<E_VehicleAssociation> vehicles;

}

And the dependent entity is:

public class E_VehicleAssociation {

    @Id
    private long id;

    @ManyToOne
    private E_Campaign campaign;

}

Is it possible to do something like this, and have that delete the dependent entity:

campaign.getVehicles().remove(0);
em.merge(campaign);

I.e., I'm removing the reference to an element of campaign.vehicles() would remove that entity from the VehicleAssociation table? More specifically, I'm interested in auto-deleting all entities that are references by a particular campaign, and want to avoid iterating and deleting.

Thank you!
  Pawel.

P.S. Any idea when JPA2.1 will be implemented by OpenJPA? :)