greuze
greuze

Reputation: 4398

Delete elements from one-to-many relationships when removing them from a JPA Entity

I am using JPA (Hibernate) with the following entity class with one one-to-many relationship.

When I add elements to the list, and then persist the Organization entity, it adds the new elements to the proyects table, but when I remove elements from the list, nothing happens when persist (or merge), and I would like these elements to be removed from the database.

I tried also orphanRemoval=true in the OneToMany annotation, but it doesn't work.

@Entity
public class Organization {     

    @Id
    @GeneratedValue
    public long internalId;

    @Basic
    @Column(nullable = false, length = 100)
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
    private List<Proyect> proyects;

    // Getters and Setters
}

Upvotes: 2

Views: 3533

Answers (1)

Thomas
Thomas

Reputation: 88707

You need to set Proyect.organization to null and update that entity, since this property is responsible for the database entry (Proyect is the owning side in this case ).

Upvotes: 1

Related Questions