Spring Data Rest (SDR) error: PersistentEntity must not be null

我怕爱的太早我们不能终老 提交于 2020-01-02 03:10:29

问题


I'm working to expose my spring data repositories via SDR. When I navigate to my rest url (http://localhost:8080/trxes), I get an error: {"cause":null,"message":"PersistentEntity must not be null!"}

On closer inspection of the spring data source, I see that the getRepositoryFactoryInfoFor() method returns empty repository information i.e.

private RepositoryFactoryInformation<Object, Serializable> getRepositoryFactoryInfoFor(Class<?> domainClass) {

    Assert.notNull(domainClass, "Domain class must not be null!");

    RepositoryFactoryInformation<Object, Serializable> repositoryInfo = repositoryFactoryInfos.get(ClassUtils
            .getUserClass(domainClass));
    return repositoryInfo == null ? EMPTY_REPOSITORY_FACTORY_INFO : repositoryInfo;
}

The probable reason for my problem is that my persistent entities inherit from a single base class, and i'm using a single table strategy as follows:

there is a TRX table in the database with a matching Trx Class. VariableIncome, VariableExpense, FixedIncome and FixedExpense all inherit from Trx and persist to the TRX table.

    @Entity
    @Table(name = "TRX")
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name = "TRX_TYPE", discriminatorType = DiscriminatorType.STRING)
    abstract public class Trx extends AbstractPersistable<Long> {

All the subclasses look similar to VariableIncome shown below:

    @Entity
    @DiscriminatorValue("VARIABLE_INCOME")
    public class VariableIncome extends Trx {

My repository setup is (no annotations on this class):

public interface TrxRepository extends CrudRepository<Trx, Long> {

I run the described setup using:

@SpringBootApplication
public class RestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestApplication.class, args);
    }

}

I guess what I'm looking for is whether there's a way of telling SDR (when it tries to deduce what my persistent classes are) that all the subclasses should map back to Trx?


回答1:


This is an issue on the "REST" side and less so on the "DATA" side.

You need to use the Jackson annotations for type information.

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include = As.PROPERTY, property = "@class")

You can find more here, as there a few ways to structure this depending on your use case and preference.



来源:https://stackoverflow.com/questions/29876749/spring-data-rest-sdr-error-persistententity-must-not-be-null

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