Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 531
Robert Ngetich Avatar asked Apr 26 '15 10:04

Robert Ngetich


1 Answers

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.

like image 71
bvulaj Avatar answered Jan 02 '23 11:01

bvulaj