【问题标题】:Spring boot hibernate problem posting json objectSpring Boot休眠问题发布json对象
【发布时间】:2019-03-01 01:13:30
【问题描述】:

我有一个与 POST JSON 对象相关的问题,以将数据持久保存在 Spring Boot 应用程序中。

实体如下所示:

@Entity
@Table(name = "extended_company_info")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ExtendedCompanyInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column(name = "city", nullable = false)
    private String city;

    @NotNull
    @Column(name = "url", nullable = false)
    private String url;

    @NotNull
    @Column(name = "phone", nullable = false)
    private String phone;


    @OneToMany(mappedBy = "extendedCompanyInfo", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<AddressInfo> addresses = new HashSet<>();

    @OneToOne
    @JoinColumn(unique = true)
    @JsonIgnore
    private Company company;

    //… getters setters

}

表格如下所示:extended_company_info (id, city, url, phone)

@Entity
@Table(name = “address_info")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AddressInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column(name = “address”, nullable = false)
    private String address;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = “address_type", nullable = false)
    private AddressType addressType;


    @JsonIgnore
    @ManyToOne(optional = false)
    @JoinColumn(name="extended_company_info_id")
    @NotNull
    private ExtendedCompanyInfo extendedCompanyInfo;

    //… getters setters

}

表格如下所示:address_info (id, address, address_type, extended_company_info_id)

public enum AddressType {
    ADDRESS1, ADDRESS2, ADDRESS3
}

我发布的 JSON 对象如下所示:

{
    “city”: “city”,
    “url”: “http://www.url.com”,
    “phone”: “123456789”,
    “addresses”: [{
        “address”: “address abcd”,
        “addressType": “ADDRESS1”
    }],
    "company": {
        "id": 1,
        "name": “Company name“,
        "shortName": “Company shortname“,
        "customerNumber": "1234"
    }
}

但我收到以下错误:

Bad Request: Validation failed for classes [package.AddressInfo] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath= extendedCompanyInfo, rootBeanClass=class package.AddressInfo, messageTemplate='{javax.validation.constraints.NotNull.message}'}

我应该以什么方式在 JSON 对象中构建 extendedCompanyInfo?

我错过了什么?

【问题讨论】:

  • extendedCompanyInfo 使用@NotNull 进行注释。删除它并再次测试。
  • 当您发布没有带有 NotNull 注释的对象的 JSON 对象时,为什么您在某些对象上有 NotNull 注释

标签: hibernate spring-boot jackson entity


【解决方案1】:

正如评论员在上面提到的: 您是说 extendedCompanyInfo 不能为空,并且 AddressInfo 和 ExtendedCompanyInfo 之间的多一关系不是可选的。

您发布的 json 表示不包括 extendedCompanyInfo 节点,因此您的验证失败。

在 JSON 帖子中包含 extendedCompanyInfo 或删除 @NotNull 和 optional = false 注释。

【讨论】:

  • 如果我从extendedCompanyInfo 中删除@NotNulloptional = false 注释它可以工作,但是address_info 表中的extended_company_info_id 列是空的,这是正确的。但我想要idextendedCompanyInfo inextended_company_info_id 列。如何在同一个 JSON 对象中实现这一点?
猜你喜欢
  • 2014-08-14
  • 2015-02-25
  • 2016-09-04
  • 2014-05-12
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2018-12-16
  • 2019-09-27
相关资源
最近更新 更多