当前位置: 首页 > 面试题库 >

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

巢德华
2023-03-14
问题内容

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

父母

@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


问题答案:

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

谁能解释它为什么起作用?



 类似资料:
  • 在Symfony 2(最新版本)下,我试图更新我的实体: 我得到了这个错误消息到我的终端: [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] 无法识别的选项“security.firewalls.access\u control”下的“0、1、2、3” 我是Symfony的新手,我不知道在哪里

  • 我需要在另一个相关实体更新后对一个实体执行更新。 我有两个实体:和,关系为1:N。两者都有字段。状态取决于所有子状态字段。因此,如果更新了一个,我需要重新计算的新状态并持久化/更新它。 我实现了一个监听器: 监听器在中进行了注释,并且正在正确调用它。但是,在流程完成后,仍然保持旧状态,即使使用正确的新状态调用。

  • 我有一个User类,它有一些附加属性: 现在,如果我调用一些方法来改变其中一个房子(例如改变它的名称属性),如果我调用,我仍然会得到房子的旧值: 我如何确保它总是更新?

  • 我有一个实体VCenterDistributedVirtualPortgroup,它扩展了VCenterNetwork,两个实体在实体VCenterFolder中处于OneToMany关系中。我得到以下错误:

  • 问题内容: 因此,我不确定如何问这个问题,因为似乎很容易找到这个问题的答案。 我有3张桌子;ContentHeader,ContentType1和ContentType2。ContentHeader具有主自动递增键。ContentType1和ContentType2都维护指向ContentHeader的主键的外键。这些外键也是它们各自表的主键。 我创建了四个类: 尝试生成架构时,这将引发空指针。我

  • 我们使用JPA实体和hibernate来实现持久性。我有一个< code >计划实体和一个< code >升级实体。当我创建一个新的升级并保持它时,计划也在不知何故地得到更新。此更新导致< code > OptimisticLockException ,并阻止进一步升级。这是代码框架- 中没有映射升级。当我运行此 我在控制台日志里看到了这个 我不希望发布有关计划的更新,因为没有任何更改。我只是使用