不能 post 具有一对一 HAL 的实体 link 使用 Spring 剩余数据 mvc

Can't post entity with one to one HAL link using Spring rest data mvc

我尝试保存 Clinic 实体 nullable = falseUser 实体的 OneToOne nullable = false 关系。

诊所实体:

//....Some fields
@OneToOne(optional=false,fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@NotNull
private User user; 
//.. Getters & Setters

用户实体

//....Some fields
@OneToOne(fetch = FetchType.LAZY,mappedBy="user",cascade=CascadeType.ALL,orphanRemoval = true)
private Clinic clinic;
//.. Getters & Setters

List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=user, rootBeanClass=class com.domain.entity.Clinic, messageTemplate='{javax.validation.constraints.NotNull.message}'} ]

感谢 @Alan Hay,我的错是我错过了在 Clinic 实体上添加 setUser() & getUser()

添加它们后效果非常好,..

curl -i -X POST -H "Content-Type: application/json" -d '{"name":"clinc","address":"address","city":"city","area":"area","user":"http://localhost:8080/clinicfinder/api/user/2"}' http://localhost:8080/clinicfinder/api/clinic

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/clinicfinder/api/clinic/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Apr 2017 15:59:16 GMT

{
  "name" : "clinc",
  "address" : "address",
  "city" : "city",
  "area" : "area",
  "longitude" : null,
  "latitude" : null,
  "createDate" : null,
  "updateDate" : null,
  "doctors" : [ ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/clinicfinder/api/clinic/1"
    },
    "clinic" : {
      "href" : "http://localhost:8080/clinicfinder/api/clinic/1"
    },
    "user" : {
      "href" : "http://localhost:8080/clinicfinder/api/clinic/1/user"
    }
  }
}