spring-projects / spring-data-redis

Provides support to increase developer productivity in Java when using Redis, a key-value store. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-redis/
Apache License 2.0
1.77k stars 1.17k forks source link

@RedisHash is returning null elements for CrudRepository.findAll() in different Spring Boot Microservices #2114

Closed KaisNeffati closed 3 years ago

KaisNeffati commented 3 years ago

Hello there Spring team,

I ve been struggling for a couple of days to find a solution for a problem am facing. I searched through the net and i was really exhausted without any results ! Until I found it on stackoverflow in an unsolved question link

So to explain the issue , here is the environment : Two microservices A and B

Both have dependencies spring boot and spring data redis version 2.3.8.RELEASE

Both have the following Repository and Entity

@Repository
public interface EventRepository  extends CrudRepository<Event, UUID> {
@Nonnull
@Override
List<Event> findAll();
}
@RedisHash("EVENT:DATASET")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Event {
private UUID id;
private NestedEvent data; // NestedEvent : Object with nested Objects
}

Application yml

spring:
redis:
host: localhost
port: 6379
password: password
ssl: false
database: 0

So the scenario is the following :

A : Store a list of Event in Redis (example 100 event) B : Fetch all events : eventRepository.findAll()
Result : List of nulls (example 100 null element)

The Fixe i made was to extract a lib and make sure the entity Event is always in the same package. TypeAlias("EVENT"): Do nothing

Please fixe the issue Thanks

christophstrobl commented 3 years ago

I'm not sure I fully understand the issue. The MappingContext can only know about @TypeAlias for entities it is aware of. By default it will scan the base package based on @EnableRedisRepositories#basePackages. There are several ways of ensuring types are discovered. Eg. by using the @EntityScan annotation or via an initial entity set.

RedisMappingContext ctx = new RedisMappingContext();
ctx.setInitialEntitySet(Collections.singleton(Event.class));
spring-projects-issues commented 3 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues commented 3 years ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

KaisNeffati commented 3 years ago

Hello,

I have made an example to illustrate the problem: https://github.com/KaisNeffati/RedisIssue

To reproduce you need :

CaptureRedis

KaisNeffati commented 3 years ago

Docker compose pour redis

version: "3.9"
services:
  ############ REDIS ############
  redis:
    container_name: redis
    image: "redis:alpine"
    command: [ "redis-server", "--appendonly", "yes", "--requirepass", "password"]
    volumes:
      - ./data/redis:/data
    ports:
      - "6379:6379"
    restart: always