Spring Data Rest - can not update (PATCH) a list of child entities that have a reference to another entity

时光毁灭记忆、已成空白 提交于 2019-12-01 20:04:55

I can't believe it!!! I added an empty constructor with String argument to Child and everything worked!

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "children")
public class Child extends LongId {

    @NonNull
    @Column(nullable = false)
    private String name;

    @NonNull
    @ManyToOne(optional = false)
    private Reference reference;

    public Child(String reference) {
    }
}

Can anyone explain why it worked?!

None of these solutions will work. Sure you can create a fake dummy constructor that accepts a string so the error will go away, but that is useless seems it does nothing! (eg it will get passed in a string uri such as localhost:8080/address/1 which is useless as no resolution happens to map it to a domain entity.

From my research there is no way to get this to work. As soon as you implement a custom controller you can no longer automatically convert a URI from the @RequestBody object to a repository managed domain entity. Basically spring data rest adds secret sauce to resolve URI's which cant be accessed in a manual controller you define.

There are probably methods you can implement to create a URI resolver but good luck with that as its more than likely undocumented.

As a workaround or 'hack'.. Try just use spring data rest repository end points, maybe even repository event hooks like @HandleBeforeSave or if that is not an option I have considered this.

This wont work as URI's wont get mapped to entities

   {
   "name": "joe",
   "addresses": ["localhost:8080/address/1",
                  "localhost:8080/address/8", 
                  "localhost:8080/address/23"]
    }

Instead create a DTO and pass in primary key which in the custom controller you do your own resolution (eg get address by Primary Key in the AddressRepository). This is quite a manual approach and obviously not ideal.

{
   "name": "joe",
   "addresses": [1, 8, 23]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!