Cyber
Cyber

Reputation: 59

Spring Data Redis: Deserializiation

I have a class of different data types. One of them is a Map of Integer keys and int array values (It has 24 cells). I store the class in Redis using Spring Data but when I get it from Redis, it gives the following error.

This is the Map:

Map<Integer, int[]> mymap = new Hashmap<>();

This is the error:

org.springframework.data.mapping.MappingException: Problem deserializing 'setterless' property ("mymap"): no way to handle typed deser with setterless yet

Is there any other way to serialize and deserialize mymap? Or I should think of other ways to store this variable?

Edited:

This is my class:

private String word;
private int DF;
private boolean NE;
private double mean;
private Map<Integer, Burst> interal = new HashMap<>();
private Map<String, Date> docs = new HashMap<>();
private Map<Integer, int[]> TWF;

And this is my redis config:

public class redisConfig {
@Primary
@Bean("rediscf1")
JedisConnectionFactory jedisConnectionFactory1() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    redisStandaloneConfiguration.setDatabase(0);

    return new JedisConnectionFactory(redisStandaloneConfiguration, new JedisConfig());
}


@Primary
@Bean(name = "redis1")
public RedisTemplate<String, Object> redisTemplate1(@Qualifier("rediscf1") JedisConnectionFactory cf) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(cf);
    return template;
}}

Upvotes: 0

Views: 1764

Answers (1)

Adam Lesiak
Adam Lesiak

Reputation: 501

My Redis config:

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    return new JedisConnectionFactory(redisStandaloneConfiguration);
}

@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory cf) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(cf);
    return template;
}

(I suggest to change here Object to class of your entity class to save)

Next I have two classes. Entity for save and TestClass like your "Burst" class. Remember to add in all saving classes implements Serializable.

public class TestEntity implements Serializable {
    private String id;
    private Map<Integer, TestClass> interal = new HashMap<>();
    private Map<Integer, int[]> TWF;

    // getters and setters
}

public class TestClass implements Serializable {
    private String name;

    // getters and setters
}

And saving data code:

    /* Initialize Hash operation*/
    String KEY = "redis-map-key";
    hashOperations = redisTemplate.opsForHash();

    /* Fill Entity to save */
    TestEntity testEntity = new TestEntity();

    Map<Integer, int[]> mapWithArray = new HashMap<>();
    int[] arr = {1, 5, 8};
    mapWithArray.put(1, arr);

    /* Internal class */
    TestClass testClass = new TestClass();
    testClass.setName("Test name");
    Map<Integer, TestClass> internal = new HashMap<>();
    internal.put(99, testClass);

    /* Fill final object */
    testEntity.setId("entity-id");
    testEntity.setTWF(mapWithArray);
    testEntity.setInteral(internal);

    /* Save entity */
    hashOperations.put(KEY, testEntity.getId(), testEntity);

    /* Load entity */
    TestEntity entityLoaded = (TestEntity) hashOperations.get(KEY, testEntity.getId());

    System.out.println("Entity ID: " + entityLoaded.getId() + ", entity array: " + entityLoaded.getTWF());

RedisTemplate is autowired.

All working fine also with other types from your class (Date, int, boolean)

Upvotes: 1

Related Questions