2014-10-02 71 views
0

使用Spring Data REST时在标题中获取错误。如何解决?无法为类X创建自连接。未找到持久实体

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,它保存到数据库正确:

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
休眠4.3.6.Final

回答

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: [email protected]; 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: [email protected]; 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: [email protected]; line: 1, column: 1]" 
} 

我建议你从PartyRepository更改程序存储库来IndividualRepository

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

您看到这个错误,因为SDR找不到仓库个人闯民宅施工时的链接。只需添加个人存储库并不导出它就可以解决您的问题。

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

Thx。 1.我需要在同一回复中回复个人以及其他党派(组织)的子类型。 2.你在你的JSON中放置了“@class”=“Individual”吗? – 2014-10-10 18:05:25

+0

抱歉没有注意到jackson annotation @class。解决您的问题是创建IndvidualRepository并设置不导出。更新我的答案。 – Stackee007 2014-10-14 18:58:07