Spring Data Rest (SDR) 错误:PersistentEntity 不能为空 [英] Spring Data Rest (SDR) error: PersistentEntity must not be null

查看:18
本文介绍了Spring Data Rest (SDR) 错误:PersistentEntity 不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过 SDR 公开我的 Spring 数据存储库.当我导航到我的 rest url (http://localhost:8080/trxes) 时,出现错误:{"cause":null,"message":"PersistentEntity 不能为空!"}

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

仔细检查 spring 数据源,我看到 getRepositoryFactoryInfoFor() 方法返回空的存储库信息,即

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:

数据库中有一个 TRX 表,有一个匹配的 Trx 类.VariableIncome、VariableExpense、FixedIncome 和 FixedExpense 都继承自 Trx 并持久化到 TRX 表中.

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

所有子类看起来都类似于下图所示的 VariableIncome:

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);
    }

}

我想我正在寻找的是是否有一种方法可以告诉 SDR(当它试图推断出我的持久类是什么时)所有子类都应该映射回 Trx?

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?

推荐答案

这是REST"方面的问题,而DATA"方面的问题较少.

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

您需要使用 Jackson 注释来获取类型信息.

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.

这篇关于Spring Data Rest (SDR) 错误:PersistentEntity 不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆