如何防止 Hibernate 删除 JSON 帖子中不存在的子对象? [英] How do I prevent Hibernate from deleting child objects not present in a JSON post?

查看:22
本文介绍了如何防止 Hibernate 删除 JSON 帖子中不存在的子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JPA Property 实体,它有子元素(多个 Rate 和多个 Reservation ).在我的 JavaScript 应用程序中,我通过 REST {property:{rates:[...], reserved[...]} 拉取 JSON.Rates 和 Reservations 非常冗长,因此当我发布属性更新(例如更改名称)时,我会从 JSON POST 负载中删除费率和预订.我希望 Hibernate 会简单地忽略丢失的键,但是唉,它在保存时删除了所有的孩子.如果它们不存在,我如何指定 Hibernate 忽略它们?

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:我对级联的理解是有限的,但我确实想要一个功能,如果有人删除了一个属性,它必须删除价格和预订.我无处可通过完整的属性保存更新价格或预订,所以也许我应该只使用 CASCADE=UPDATE?价格有自己的更新机制,预订也是如此.

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.

推荐答案

Late answer,但是如果您使用的是 Spring MVC Rest 控制器而不是 Spring Data Rest 控制器来处理更新,那么看起来前者不支持部分更新/补丁请求.

Late answer, however if you are using a Spring MVC Rest controller to handle the update rather than a Spring Data Rest controller then it would appear the former does not support partial updates/patch requests.

使用传统的非 Restful Web 应用程序直接通过表单修改实体当然是可能的.例如:

Working with traditional non-restful web apps modifying entities directly via a form this is of course possible. For example:

@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
} 

但是,当通过@RequestBody 将 JSON 绑定到实体时,这是不可能的:

However when binding JSON to an Entity via @RequestBody this is not possible:

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

围绕此有一些出色的 JIRA:

There are some outstanding JIRAs around this:

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

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

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

以及各种 SO 问题:

And various SO questions:

Spring 部分更新对象数据绑定

好消息是 Spring Data Rest 确实支持通过补丁进行部分更新,因此如果可以选择将您的存储库公开为 Rest 存储库,那么您应该能够实现所需的行为:

The good news however is that Spring Data Rest does support partial updates via patch so if it is an option to expose your repository as a Rest Repository then you should be able to achieve the required behaviour:

https://spring.io/guides/gs/accessing-data-休息/

PUT 替换整个记录.未提供的字段将被替换与空.PATCH 可用于更新项目的子集.

PUT replaces an entire record. Fields not supplied will be replaced with null. PATCH can be used to update a subset of items.

这篇关于如何防止 Hibernate 删除 JSON 帖子中不存在的子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆