大佬教程收集整理的这篇文章主要介绍了Spring Data Rest-无法更新(PATCH)引用另一个实体的子实体列表,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我简直不敢相信!!!我向Child添加了一个带有String参数的空构造函数,一切正常!
@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) {
}
}
谁能解释它为什么起作用?
我有三个实体:父级,子级和一些参考:
父母
@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> {
}
在此之前,我坚持了几个有参考文献的孩子。然后,我创建了一个有一个孩子的新父母:
POST http://localhost:8080/api/parents
{
"name" : "parent2","children" : [
"http://localhost:8080/api/children/3"
]
}
并成功获得状态201已创建。但是,当我尝试将另一个孩子添加到 parent2时 (用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]"
}
如果我从子级删除到参考实体的链接:
@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中 。
您能指出我如何正确引用子实体的列表吗?
这个例子的仓库在这里:https://github.com/Cepr0/restdemo
以上是大佬教程为你收集整理的Spring Data Rest-无法更新(PATCH)引用另一个实体的子实体列表全部内容,希望文章能够帮你解决Spring Data Rest-无法更新(PATCH)引用另一个实体的子实体列表所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。