Существует следующий DTO с EnumMap
@RedisHash("student")
@Data
public class Student {
@Id
String id;
String firstname;
String lastname;
EnumMap<AdressType, Address> addresses;
}
AdressType
типа Enum
:
public enum AdressType {
Home,
Work;
}
Объявление репо Spring-Data для работы с Student
import org.springframework.data.repository.CrudRepository;
public interface StudentRepository extends CrudRepository<Student, String> {
}
Теперь давайте сохраним Student
в Redis и извлечем его по id
сразу после сохранения:
private final StudentRepository studentRepository;
public Student writeAndRead() {
EnumMap<AdressType, Address> adresses = new EnumMap<>(AdressType.class);
adresses.put(AdressType.Home, new Address("USA", "New-York"));
adresses.put(AdressType.Work, new Address("USA", "San-Francisco"));
Student andrew = new Student();
andrew.setFirstname("Andrew");
andrew.setLastname("Jonson");
andrew.setAddresses(adresses);
Student andrewSaved = studentRepository.save(andrew);
Optional<Student> foundAndrew = studentRepository.findById(foundAndrew.getId());
.....
}
Код выдает следующую ошибку:
java.lang.IllegalArgumentException: Cannot create EnumMap for unknown key type
at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.core.CollectionFactory.createMap(CollectionFactory.java:311) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.core.CollectionFactory.createMap(CollectionFactory.java:266) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.readMapOfComplexTypes(MappingRedisConverter.java:885) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.readProperty(MappingRedisConverter.java:277) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.lambda$readInternal$0(MappingRedisConverter.java:244) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:332) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.readInternal(MappingRedisConverter.java:236) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.read(MappingRedisConverter.java:183) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.read(MappingRedisConverter.java:117) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.core.RedisKeyValueAdapter.get(RedisKeyValueAdapter.java:300) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.lambda$findById$3(KeyValueTemplate.java:250) ~[spring-data-keyvalue-2.0.10.RELEASE.jar:2.0.10.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:343) ~[spring-data-keyvalue-2.0.10.RELEASE.jar:2.0.10.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.findById(KeyValueTemplate.java:248) ~[spring-data-keyvalue-2.0.10.RELEASE.jar:2.0.10.RELEASE]
at org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository.findById(SimpleKeyValueRepository.java:126) ~[spring-data-keyvalue-2.0.10.RELEASE.jar:2.0.10.RELEASE]