我必须使用Spring Redis在Redis中存储一个对象。对象是
@RedisHash(value = "shoppingcart")
public class ShoppingCart {
@Id
private String cartGuid;
private Map<String, List<CartItemDto>> promotionProductMap = new LinkedHashMap<String, List<CartItemDto>>();
private LinkedList<CartItemDto> cartItems = new LinkedList<>();
....
}
CartItemDto看起来像这样
public class CartItemDto implements Serializable {
private static final long serialVersionUID = -7047005024550867744L;
private String cartItemId;
private Long productId;
...
}
我有一个存储库可以保存到Redis
@Repository
public interface ShoppingCartRepository extends CrudRepository<ShoppingCart, String> {
}
但是当我试图通过
存储shoppingCart时shoppingCartRepository.save(shoppingCart);
抛出异常
Caused by: java.lang.IllegalArgumentException: Target bean of type java.util.LinkedList is not of type of the persistent entity (com.test.CartItemDto)!
at org.springframework.util.Assert.isTrue(Assert.java:68) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:397) ~[spring-data-commons-1.12.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.PathIndexResolver.doResolveIndexesFor(PathIndexResolver.java:92) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.PathIndexResolver.access$000(PathIndexResolver.java:50) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.PathIndexResolver$1.doWithPersistentProperty(PathIndexResolver.java:115) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.PathIndexResolver$1.doWithPersistentProperty(PathIndexResolver.java:95) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:312) ~[spring-data-commons-1.12.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.PathIndexResolver.doResolveIndexesFor(PathIndexResolver.java:95) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.PathIndexResolver.resolveIndexesFor(PathIndexResolver.java:79) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:357) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:109) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.redis.core.RedisKeyValueAdapter.put(RedisKeyValueAdapter.java:183) ~[spring-data-redis-1.7.5.RELEASE.jar:?]
at org.springframework.data.keyvalue.core.KeyValueTemplate$1.doInKeyValue(KeyValueTemplate.java:168) ~[spring-data-keyvalue-1.1.5.RELEASE.jar:?]
at org.springframework.data.keyvalue.core.KeyValueTemplate$1.doInKeyValue(KeyValueTemplate.java:158) ~[spring-data-keyvalue-1.1.5.RELEASE.jar:?]
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:377) ~[spring-data-keyvalue-1.1.5.RELEASE.jar:?]
... 156 more
如果我删除了promotionProductMap,它可以保存shoppingCart 我不知道为什么它不能保存Hashmap,可以使用List 任何人都知道如何将Hashmap保存到Redis?