How to prevent Hibernate from deleting children not present in JSON mail?

I have a JPA object Propertythat has children (multiple Rateand multiple Reservation). In my JavaScript application, I pull JSONthrough REST {property:{rates:[...], reservations[...]}. Rates and reservations are very detailed, so when I publish a property update (for example, a name change), I remove the rates and reservations from the payload JSON POST. I was hoping that Hibernate would simply ignore the missing keys, but alas, it would remove all the children in the rescue. How do I tell Hibernate to ignore them if they are not?

@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: , , , - , . , , , CASCADE = UPDATE? , .

0
1

, , Spring MVC Rest , Spring Data Rest, , , /.

-, , , , . :

@RequestMapping
public String updateEntity(@ModelAttribute myEntity){
  //submitted form parameters merged to existing entity 
  //loaded via getMyEntity() leaving unmodified fields as they were
}

@ModelAttribute
public MyEntity getMyEntity(){
  //load some existing entity
} 

JSON Entity @RequestBody :

@RequestMapping
public String updateEntity(@RequestBody myEntity){
  //new instance instantiated by the Jackson mapper
  //missing fields will be null
}

JIRA:

https://jira.spring.io/browse/SPR-13127

https://jira.spring.io/browse/SPR-10552

https://jira.spring.io/browse/SPR-13127

:

Spring

, Spring Data Rest , , , :

https://spring.io/guides/gs/accessing-data-rest/

PUT . , , . PATCH .

0

Source: https://habr.com/ru/post/1664117/


All Articles