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

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

问题描述

我有一个JPA 属性实体,它拥有子元素(多个 Rate 和多个预订的)。在我的JavaScript应用程序中,我通过 REST {property:{rates:[> JSON >) ..],保留[...]} 。费率和预订非常冗长,所以当我发布属性更新(如更改名称)时,我从 JSON POST 有效内容中删除了费率和预订。我希望Hibernate能简单地忽略丢失的键,但唉,它会在保存时删除所有的孩子。

  @Entity 
public class Property {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私有长ID;

字符串名称;

@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?速度有自己的更新机制,所以做保留。

解决方案

但是,如果您使用Spring MVC Rest控制器处理更新而不是Spring Data Rest控制器,那么它会显示前者不支持部分更新/修补请求。



使用传统的非宁静web应用程序修改实体直接通过表格这当然是可能的。例如:

  @RequestMapping 
public String updateEntity(@ModelAttribute myEntity){
// submit form参数合并到现有实体
//通过getMyEntity()加载,保留未修改的字段,因为它们是
}

@ModelAttribute
MyEntity getMyEntity(){$ b $然而,当通过@绑定JSON到一个实体时, RequestBody这是不可能的:

  @RequestMapping 
public String updateEntity(@RequestBody myEntity){
/ /由Jackson映射程序实例化的新实例
//缺少字段将为空
}





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



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



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



以及各种SO问题:



Spring部分更新对象数据绑定

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



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


< PUT替换整个记录。未提供的字段将被替换为
,且为空。 PATCH可用于更新项目子集


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.

解决方案

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.

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
} 

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
}

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

And various SO questions:

Spring Partial Update Object Data Binding

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-rest/

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天全站免登陆