use*_*096 11 java orm hibernate spring-data-jpa spring-data-rest
我正在尝试做一个简单的Spring应用程序.它需要公开REST端点并将其保存到关系数据库.
我参加了你的示例项目,http://spring.io/guides/gs/accessing-data-rest/.我可以按照指南中的说明进行所有操作(POST,PATCH,PUT,GET).
但是我尝试创建添加到Person Entity类的关系,事情开始分崩离析.
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
@OneToOne(cascade = {CascadeType.ALL})
private PersonDetails personDetails;
@OneToOne(cascade = {CascadeType.ALL})
private PersonChildren personChildren;
///Getter and setters for everything except id.
}
@Entity
public class PersonChildren {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String childFirstName;
private String childLastName;
@OneToOne(mappedBy="personChildren", optional=false)
private Person person;
///Getter and setters for everything except id.
}
@Entity
public class PersonDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String email;
private String phoneNumber;
@OneToOne(mappedBy="personDetails",optional=false)
private Person person;
///Getter and setters for everything except id.
}
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)
build.gradle
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-accessing-data-rest'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/libs-release" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
compile("org.springframework.data:spring-data-rest-webmvc")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Run Code Online (Sandbox Code Playgroud)
呼叫:
$ curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName":"John", "lastName": "Doe", "personDetails": { "email": "john@gmail.com", "phoneNumber": "001-002-0003" }, "personChildren": {"childFirstName": "Mary", "childLastName": "Martin" } }' <code> http://localhost:8080/people </code>
Response:
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
<code>
Location: http://localhost:8080/people/1
</code>
Content-Length: 0
Date: Thu, 26 Jun 2014 05:42:45 GMT
$ curl http://localhost:8080/people
{
"timestamp" : 1403761371011,
"status" : 500,
"error" : "Internal Server Error",
"exception" : "org.springframework.http.converter.HttpMessageNotWritableException",
"message" : "Could not write JSON: Detected multiple association links with same relation type! Disambiguate association @javax.persistence.OneToOne(optional=false, targetEntity=void, cascade=[], fetch=EAGER, orphanRemoval=false, mappedBy=personChildren) private com.ds.dao.rest.Person com.ds.dao.rest.PersonChildren.person using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.UnmodifiableMap[\"people\"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Detected multiple association links with same relation type! Disambiguate association @javax.persistence.OneToOne(optional=false, targetEntity=void, cascade=[], fetch=EAGER, orphanRemoval=false, mappedBy=personChildren) private com.ds.dao.rest.Person com.ds.dao.rest.PersonChildren.person using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.UnmodifiableMap[\"people\"]->java.util.ArrayList[0])",
"path" : "/people"
}
Run Code Online (Sandbox Code Playgroud)
问题1:我能够发帖,但我的GET一直都失败了.
问题2:发布成功后,为什么会出现此错误?
问题3:是否有一个好的Spring指南可以帮助REST和JPA?如果您仍在使用这些模块,我可以查看哪些示例?
问题4:@RepositoryRestResource是否存在问题?除非我将spring-data-rest-webmvc添加为依赖项,否则无法识别.
这类似于未解决的问题 Spring Data Rest Ambiguous Association Exception
更新:
它只OneToOne在Person课堂上使用一个映射.如果我想补充的两个类,personDetails并personChildren在Person与OneToOne映射.它不起作用.
我也尝试添加@JointColumn(name="person_details")和@JointColumn(name="person_children")以personDetails和personChildren.它也没用.
Oli*_*ohm 14
原因很简单:关联实体的关系名称是从包含类的属性名称派生的.因此,无论PersonDetails并且PersonChildren要创建出站链接到一个Person名为person.如果我们渲染它,它看起来像这样
{ _links : {
person : { href : … }, <- the one from PersonDetails
person : { href : … } <- the one from PersonChildren
}
Run Code Online (Sandbox Code Playgroud)
这当然是无效的.此外,排列阵列中的两个链接将不允许您再区分两个链接(哪一个来自PersonDetails哪个,哪个来自哪个PersonChildren.
所以这里有几个选项:
Person属性,@RestResource并将注释的属性配置为rel不同的属性person.exported标志设置为,@RestResource即可false呈现链接.如果指针(例如from PersonDetails)仅在代码中相关,但实际上不在JSON表示中,则这可能很有用.| 归档时间: |
|
| 查看次数: |
5097 次 |
| 最近记录: |