1
一對一HAL鏈接我試圖挽救診所實體OneToOne nullable = false
關係與用戶實體發佈實體。不能使用彈簧安置數據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
列表:[ ConstraintViolationImpl {interpolatedMessage = '不能爲空',的PropertyPath = 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。 添加它們後,它現在工作。感謝您的親切幫助,真的節省了我的時間。 –