Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sebastienvermeille opened this issue Mar 11, 2021 · 1 comment
Closed
Assignees
Labels
type: bug A general bug

Comments

@sebastienvermeille
Copy link

sebastienvermeille commented Mar 11, 2021

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 11, 2021
@mp911de mp911de self-assigned this Mar 11, 2021
@mp911de
Copy link
Member

mp911de commented Mar 11, 2021

The problem here is that our RedisKeyValueAdapter is called with the keyspace only. The type hint from the repository is lost as the adapter interface doesn't accept the desired entity type.

Going further, the type hits are unknown because Dog and Cat are not registered in RedisMappingContext (RedisMappingContext.setInitialEntitySet(…)).

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 11, 2021
mp911de added a commit that referenced this issue Mar 11, 2021
We now accept a type hint when calling getAllOf(…) to avoid materializing null instances when the actual typehint cannot resolve to a entity.

Closes #1995
@christophstrobl christophstrobl added this to the 2.5 M5 (2021.0.0) milestone Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants