1
를 사용하여 HAL 링크 하나에 함께 실체를 게시 할 수 없습니다.내가 <strong>사용자</strong> 엔티티와 OneToOne <code>nullable = false</code> 관계와 <strong>클리닉</strong> 개체를 저장하려고 한 봄 나머지 데이터 MVC
클리닉 법인 :
//....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
후 사용.
curl -i -X POST -H "Content-Type: application/json" -d '{"firstName" : "Khaled","lastName" : "Lela","userName" : "KhaledLela","password" : "128ecf542a35ac5270a87dc740918404","email" : "[email protected]", "phone" : "12345678","gender" : "MALE", "level" : "ADMIN"}' http://localhost:8080/clinicfinder/api/user HTTP/1.1 201 Created Server: Apache-Coyote/1.1 Location: http://localhost:8080/clinicfinder/api/user/4 Content-Type: application/hal+json;charset=UTF-8 Transfer-Encoding: chunked Date: Thu, 20 Apr 2017 12:26:41 GMT { "token" : null, "firstName" : "Khaled", "lastName" : "Lela", "userName" : "KhaledLela", "password" : "128ecf542a35ac5270a87dc740918404", "email" : "[email protected]", "phone" : "12345678", "gender" : "MALE", "level" : "ADMIN", "birthDate" : null, "createDate" : null, "updateDate" : null, "_links" : { "self" : { "href" : "http://localhost:8080/clinicfinder/api/user/4" }, "user" : { "href" : "http://localhost:8080/clinicfinder/api/user/4" }, "clinic" : { "href" : "http://localhost:8080/clinicfinder/api/user/4/clinic" } } }
수 제약 조건 위반의 사용자 링크되지 포스트 클리닉
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
목록 : propertyPath 'null이 될 수 없습니다'[ ConstraintViolationImpl {interpolatedMessage = = user, rootBeanClass = class com.domain.entity.Clinic, messageTemplate = '{javax.validation.constraints.NotNull.message}'} ]
클리닉 저장소
@RepositoryRestResource(path = "clinic") public interface ClinicRepo extends CrudRepository<Clinic, Long> {}
응원단
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> <spring.data.jpa.version>1.11.1.RELEASE</spring.data.jpa.version> <spring.data.rest.webmvc.version>2.6.1.RELEASE</spring.data.rest.webmvc.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- Spring Rest Repository --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${spring.data.jpa.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-webmvc</artifactId> <version>${spring.data.rest.webmvc.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency>
당신은 확실히'SETUSER 클리닉에서() '(또는'인 getUser()') 방법이 .. 마법처럼 일했다? –
@AlanHay OMG, IDE setter 및 getter 생성에'setUser() 및 getUser())가 누락되었습니다. getter 및 setter 생성 후이 속성을 추가 할 수 있습니다. 그리고 그들을 추가 한 후 이제 작동 중입니다. 친절한 도움을 주셔서 감사합니다. 정말 내 시간을 절약했습니다. –