Skip to content

@TypeAlias used in sub objects only works for save not load #1995

Closed
@sebastienvermeille

Description

@sebastienvermeille

based on: https://gitter.im/spring-projects/spring-data?at=604878ed22a5ce4a91464a41

Assuming I have the following structure:

@RedisHash("Animal")
@TypeAlias("Cat")
public class Cat extends Animal {}

@RedisHash("Animal")
@TypeAlias("Dog")
public class Dog extends Animal {}

@RedisHash("Animal")
@TypeAlias("Animal")
public abstract class Animal {
  @Id
  @Indexed
  @NonNull
  protected String id;
}

class AnimalWrapper {
   @Id
    private String id;
     private Animal animal;
}

What is persisted in redis:

keys list

127.0.0.1:6379> keys *
1) "Animal"
2) "Animal:animal.id:c2873cc8-6dba-4fdc-9143-f642f6815f3d"
3) "Animal:dog-id:idx"
4) "Animal:cat-id:idx"
5) "Animal:dog-id"
6) "Animal:animal.id:f86eaa67-4910-4b66-afbe-f6b02576fa53"
7) "Animal:cat-id"

inside of Animal:dog-id

127.0.0.1:6379> HGETALL Animal:dog-id
 1) "_class"
 2) "Animal"
 3) "id"
 4) "dog-id"
 5) "animal._class"
 6) "Dog"
 7) "animal.postmanCounter"
 8) "2"
 9) "animal.id"
10) "f86eaa67-4910-4b66-afbe-f6b02576fa53"

Everything looks ok.
There is no issue to persist the objects I can see them gracefully persisted into redis with the correct TypeAlias.

The problem is when I try to retrieve the data.

  1. retrieving them right after persisting them -> fine no problem (cf: see the unit test in the attached repository)
  2. retrieving them without persisting them before (already persisted in redis db) ->
java.lang.InstantiationError: com.example.springdataredisexample.models.Animal

	at com.example.springdataredisexample.models.Animal_Instantiator_csbm6u.newInstance(Unknown Source)
	at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:238)
	at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:87)
	at org.springframework.data.redis.core.convert.MappingRedisConverter.doReadInternal(MappingRedisConverter.java:234)
	at org.springframework.data.redis.core.convert.MappingRedisConverter.readInternal(MappingRedisConverter.java:192)
	at org.springframework.data.redis.core.convert.MappingRedisConverter.readProperty(MappingRedisConverter.java:299)
	....

How to reproduce:

  1. clone the following repo: https://github.com/sebastienvermeille/bugreport-spring-data-redis
  2. deploy a redis server (i.e: docker run --name some-redis -p 6379:6379 -d redis)
  3. execute only the unit test: findAllShouldReturn2AnimalsWhenSavedACatAndADog this will persist 2 animals
  4. stop the spring app
  5. execute only the unit test: findAllShouldAlsoReturnAnimalsWhenTheyExistAlreadyInDb
  6. check in redis you should still have the previous animals persisted
  7. but the unit test fails as it generate an exception on invoking findAll()

I stay available in case I could eventually help.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions