Spring Data Rest - 无法更新(修补)引用另一个实体的子实体列表

2023-12-12

我有三个实体:父实体、子实体和一些参考:

Parent

@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<>();
}

Child

@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 Created。 但是当我尝试添加另一个孩子时parent2(用补丁更新):

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


我简直不敢相信! 我向 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) {
    }
}

谁能解释为什么它有效?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Data Rest - 无法更新(修补)引用另一个实体的子实体列表 的相关文章

随机推荐