I have a JPA Property
entity that has children (multiple Rate
's and multiple Reservation
's). In my JavaScript application, I pull JSON
via REST
{property:{rates:[...], reservations[...]}
. Rates and Reservations are very verbose, so when I post a property update (like changing the name), I delete the rates and reservations from the JSON POST
payload. I hoped that Hibernate would simply ignore the missing keys, but alas, it's removing all the children on save. How do I specify to Hibernate to ignore them if they're not there?
@Entity
public class Property {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
String name;
@OneToMany(mappedBy = "property", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JsonManagedReference
private Set<SeasonRate> rates = new HashSet<>();
@OneToMany(mappedBy = "property", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<Reservation> reservations = new HashSet<>();
}
Ps: My understanding of cascades is limited, but I do actually want the functionality that if someone deletes a property, it must delete the rates and reservations. Nowhere do I update rates or reservations via a full property save though, so perhaps I should just use CASCADE=UPDATE? Rates have their own update mechanism and so do reservations.