无法为 X 类创建自链接.未找到持久实体 [英] Cannot create self link for class X. No persistent entity found
问题描述
使用 Spring Data REST 时出现标题错误.如何解决?
Getting the error in the title when using Spring Data REST. How to resolve?
Party.java:
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:
Individual.java:
@Entity
public class Individual extends Party {
private String gender;
@Override
public String toString() {
return gender + " " + super.toString();
}
...getters, setters...
}
PartyRepository.java:
PartyRepository.java:
public interface PartyRepository extends JpaRepository<Party,Long> {
}
如果我发布,它会正确保存到数据库:
If I POST, it saves to the db correctly:
POST /parties {"@class":"com.example.Individual", "name":"Neil", "gender":"MALE"}
但返回 400 错误:
But returns a 400 error:
{"cause":null,"message":"Cannot create self link for class com.example.Individual! No persistent entity found!"}
从存储库中检索后看起来它是个人:
It looks like it's an Individual after retrieving from repository:
System.out.println(partyRepository.findOne(1L));
//output is MALE 1 Neil
看起来 Jackson 可以确定它是一个人:
Looks like Jackson can figure out that it's an individual:
System.out.println( new ObjectMapper().writeValueAsString( partyRepository.findOne(1L) ) );
//output is {"@class":"com.example.Individual", "id":1, "name":"Neil", "gender":"MALE"}
为什么 SDR 想不通?
Why can SDR not figure it out?
如何解决?最好使用 XML 配置.
How to fix? Preferably with XML config.
版本:
SDR 2.2.0.RELEASE
SD JPA 1.7.0.RELEASE
休眠 4.3.6.Final
Versions:
SDR 2.2.0.RELEASE
SD JPA 1.7.0.RELEASE
Hibernate 4.3.6.Final
推荐答案
SDR 存储库需要一个非抽象实体,在您的情况下它将是个人.您可以在 google 或此处搜索有关 SDR 为何需要非抽象实体的解释.
SDR repositories expect a non-abstract entity, in your case it would be Individual. You can google or search here for explaination on why SDR expects a non-abstract entity.
我自己尝试了您的代码,SDR 甚至无法用于 POST,我看到以下错误消息.
I tried your code myself and SDR won't even work for POST and I see below error message.
{
"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
I suggest you change respository from PartyRepository to IndividualRepository
public interface IndividualRepository extends JpaRepository<Individual,Long> {
}
您看到该错误是因为 SDR 在构建链接时找不到引用个人的存储库.只需添加单个存储库而不导出它就可以解决您的问题.
You are seeing that error since SDR could not find a repository refering Individual while constructing links. Simply adding Individual respository and not exporting it would solve your problem.
@RepositoryRestResource(exported = false)
public interface IndividualRepository extends JpaRepository<Individual,Long> {
}
这篇关于无法为 X 类创建自链接.未找到持久实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!