发布
社区首页 >问答首页 >无法使用Spring数据mvc发布具有一对一HAL链接的实体

无法使用Spring数据mvc发布具有一对一HAL链接的实体
EN

Stack Overflow用户
提问于 2017-04-20 12:58:37
回答 1查看 780关注 0票数 1

我试图保存诊所实体与OneToOne nullable = false关系与用户实体。

临床实体:

代码语言:javascript
代码运行次数:0
复制
//....Some fields
@OneToOne(optional=false,fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@NotNull
private User user; 
//.. Getters & Setters

用户实体

代码语言:javascript
代码运行次数:0
复制
//....Some fields
@OneToOne(fetch = FetchType.LAZY,mappedBy="user",cascade=CascadeType.ALL,orphanRemoval = true)
private Clinic clinic;
//.. Getters & Setters

违反约束的列表: ConstraintViolationImpl{interpolatedMessage='may not null',propertyPath=user,rootBeanClass=class com.domain.entity.Clinic,messageTemplate='{javax.validation.constraints.NotNull.message}'}

  • 诊所仓库 @RepositoryRestResource(path =“诊所”)公共接口ClinicRepo扩展CrudRepository {}
  • Pom UTF-8 1.6 1.6 1.11.1.RELEASE 2.6.1.RELEASE mysql -连接器-java 5.1.41运行时javax.servlet javax.servlet-api 3.0.1提供了org.springframework.data spring-data-jpa ${spring.data.jpa.version} org.springframework.data spring-data-rest-webmvc ${spring.data.rest.webmvc.version} org.hibernate hibernate-core 5.2.10.Finalorg.hibernate hibernate-验证器5.4.1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-20 16:25:11

多亏了@Alan ,我错过了在诊所实体中添加setUser() & getUser()的错误。

在加入它们之后,就像一种魅力,..

代码语言:javascript
代码运行次数:0
复制
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"
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43520267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档