Cannot create self link for class X. No persistent entity found

别说谁变了你拦得住时间么 提交于 2019-12-11 22:07:34

问题


Getting the error in the title when using Spring Data REST. How to resolve?

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

If I POST, it saves to the db correctly:

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

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

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

Why can SDR not figure it out?

How to fix? Preferably with XML config.

Versions:
SDR 2.2.0.RELEASE
SD JPA 1.7.0.RELEASE
Hibernate 4.3.6.Final


回答1:


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.

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

I suggest you change respository from PartyRepository to IndividualRepository

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

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


来源:https://stackoverflow.com/questions/26171008/cannot-create-self-link-for-class-x-no-persistent-entity-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!