Spring Data Rest - 无法更新(PATCH)具有对另一个实体的引用的子实体的列表 [英] Spring Data Rest - can not update (PATCH) a list of child entities that have a reference to another entity
问题描述
我有三个实体:父,其子和一些参考:
I have three entities: Parent, its Child and some Reference:
父
@Entity
@Table(name = "parents")
public class Parent extends LongId {
@NonNull
@Column(nullable = false)
private String name = "Undefine";
@NonNull
@OneToMany(cascade = MERGE)
private List<Child> children = new ArrayList<>();
}
儿童
@Entity
@Table(name = "children")
public class Child extends LongId {
@NonNull
@Column(nullable = false)
private String name;
@NonNull
@ManyToOne(optional = false)
private Reference reference;
}
参考
@Entity
@Table(name = "references")
public class Reference extends LongId {
@NotEmpty
@Column(nullable = false)
@Length(min = 3)
@NonNull
private String description;
}
及其存储库:
@RepositoryRestResource
public interface ParentRepo extends JpaRepository<Parent, Long> {
}
@RepositoryRestResource
public interface ChildRepo extends JpaRepository<Child, Long> {
}
@RepositoryRestResource
public interface ReferenceRepo extends JpaRepository<Reference, Long> {
}
事先我坚持了几个参考儿童。然后我创建了一个有一个孩子的新父母:
Beforehand I persisted several Children with References. Then I created a new Parent with one child:
POST http://localhost:8080/api/parents
{
"name" : "parent2",
"children" : [
"http://localhost:8080/api/children/3"
]
}
并且已成功获得状态201已创建。
但是当我尝试将另一个孩子添加到 parent2 时(用PATCH更新它):
And have successfully got status 201 Created. But when I try to add another child to parent2 (update it with PATCH):
PATCH http://localhost:8080/api/parents/2
{
"name" : "parent2",
"children" : [
"http://localhost:8080/api/children/3",
"http://localhost:8080/api/children/4"
]
}
我收到错误:
{
"cause": {
"cause": null,
"message": "Can not construct instance of restsdemo.domain.entity.Child: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/children/4')\n at [Source: N/A; line: -1, column: -1]"
},
"message": "Could not read payload!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of restsdemo.domain.entity.Child: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/children/4')\n at [Source: N/A; line: -1, column: -1]"
}
如果我删除链接到来自Child的参考实体:
If I remove link to Reference entity from Child:
@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;
}
一切正常 - child4 成功添加到 parent2 。
all works perfectly - child4 was successfully adds to parent2.
如果他们引用了其他实体,您能指点我如何正确更新子实体列表吗?
Could you point me how to correctly update a list of child entities if they have reference to another entities?
这个例子的回购在这里: https://github.com/Cepr0/ restdemo
Repo with this example is here: https://github.com/Cepr0/restdemo
推荐答案
我简直不敢相信!!!
我为Child添加了一个带有String参数的空构造函数,一切正常!
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?!
这篇关于Spring Data Rest - 无法更新(PATCH)具有对另一个实体的引用的子实体的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!