小编典典

Spring Data REST-检测到多个具有相同关系类型的关联链接

hibernate

我正在尝试做一个简单的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);

}

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'
}

呼叫:

$ 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"
}

问题1:我可以发表文章,但我的GET一直失败。

问题2:发布成功后,为什么会出现此错误?

问题3:是否有很好的Spring指南对REST和JPA有帮助?如果您仍在使用这些模块,我可以看什么示例?

问题4:@RepositoryRestResource是问题吗?除非我添加spring-data-rest-webmvc作为依赖项,否则无法识别。

这类似于未回答的问题, Spring Data
Rest歧义关联异常

更新

它仅OneToOnePerson类中使用一个映射。如果我想补充的两个类,personDetailspersonChildrenPersonOneToOne映射。它不起作用。

我也尝试添加@JointColumn(name="person_details")@JointColumn(name="person_children")personDetailspersonChildren。它也不起作用。


阅读 254

收藏
2020-06-20

共1个答案

小编典典

这样做的原因很简单:关联实体的关系名称是从包含类的属性名称派生的。因此,无论PersonDetails并且PersonChildren要创建出站链接到一个Person名为person。如果我们渲染的话,看起来会像这样

{ _links : { 
   person : { href : … }, <- the one from PersonDetails
   person : { href : … }  <- the one from PersonChildren
}

这当然是无效的。而且,将两个链接排列在一个数组中将使您无法再区分两个链接(哪个来自PersonDetails哪个链接,哪个来自哪个链接PersonChildren。)。

因此,这里有一些选择:

  1. 手动命名这些类型的关系 。您可以使用注释Person属性,@RestResource并将注释的属性配置为rel以外的其他值person
  2. 您希望两者都不输出 。可以使用完全相同的注释来完全阻止链接的呈现。只需将exported标志设置@RestResourcefalse,链接将不会呈现。如果例如from的指针PersonDetails仅在代码内相关,而实际上不在JSON表示中,则这可能很有用。
2020-06-20