无法为类X创建自链接。找不到持久实体

时间:2014-10-02 23:07:54

标签: spring-data spring-data-jpa spring-data-rest

使用Spring Data REST时在标题中获取错误。如何解决?

Party.java:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, property="@class")
@JsonSubTypes({ @JsonSubTypes.Type(value=Individual.class, name="Individual") })
public abstract class Party {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  protected Long id;

  protected String name;

  @Override 
  public String toString() {
    return id + " " + name;
  }

  ...getters, setters...
}

Individual.java:

@Entity
public class Individual extends Party {

  private String gender;

  @Override
  public String toString() {
    return gender + " " + super.toString();
  }

  ...getters, setters...
}

PartyRepository.java:

public interface PartyRepository extends JpaRepository<Party,Long> {
}

如果我发布,它会正确保存到数据库:

POST /parties {"@class":"com.example.Individual", "name":"Neil", "gender":"MALE"}

但是返回400错误:

{"cause":null,"message":"Cannot create self link for class com.example.Individual! No persistent entity found!"}

从存储库中检索后,它看起来像是个人:

System.out.println(partyRepository.findOne(1L)); 
//output is MALE 1 Neil

看起来杰克逊可以发现它是个人:

System.out.println( new ObjectMapper().writeValueAsString( partyRepository.findOne(1L) ) );
//output is {"@class":"com.example.Individual", "id":1, "name":"Neil", "gender":"MALE"}

为什么SDR无法解决这个问题?

如何解决?最好使用XML配置。

版本:
SDR 2.2.0.RELEASE
SD JPA 1.7.0.RELEASE
Hibernate 4.3.6.Final

1 个答案:

答案 0 :(得分:1)

SDR存储库期望一个非抽象的实体,在您的情况下它将是个体。您可以谷歌搜索或在此搜索SDR期望非抽象实体的原因。

我自己尝试了你的代码,SDR甚至不能用于POST,我看到下面的错误信息。

{
    "cause": {
        "cause": null,
        "message": "Can not construct instance of com.spring.data.rest.test.Party, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: org.apache.catalina.connector.CoyoteInputStream@30217e25; line: 1, column: 1]"
    },
    "message": "Could not read JSON: Can not construct instance of com.spring.data.rest.test.Party, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: org.apache.catalina.connector.CoyoteInputStream@30217e25; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.spring.data.rest.test.Party, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: org.apache.catalina.connector.CoyoteInputStream@30217e25; line: 1, column: 1]"
}

我建议您将存储库从PartyRepository更改为IndividualRepository

public interface IndividualRepository extends JpaRepository<Individual,Long> {
}

您看到该错误,因为SDR在构建链接时无法找到引用个人的存储库。只需添加Individual respository而不是导出它就可以解决您的问题。

@RepositoryRestResource(exported = false)
public interface IndividualRepository extends JpaRepository<Individual,Long> {
}