我想缓存基于接口(interface)的投影的结果
但我收到了这个错误
Caused by: com.hazelcast.nio.serialization.HazelcastSerializationException: Failed to serialize 'java.util.ArrayList'
@Repository
public interface CustomerRepository extends JpaRepository<CustomerRepository, Long> {
@Cacheable(value = "customer")
@Query("select c.first_name as firstName from customer where customer_id in :customerId")
List<NamesOnly> findByCustomerId( @Param("customerId") List<String> customerId);
}
public interface NamesOnly extends Serializable {
String getCustomerFirstName();
}
扩展Serialized
似乎不起作用
最佳答案
这是一个 Spring 问题,隐藏在深处的是:
java.io.NotSerializableException: org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor
可能是 this 的另一个出现
作为解决方法,您可以更改
List<NamesOnly> findByCustomerId( @Param("customerId") List<String> customerId);
到
List<String> findByCustomerId( @Param("customerId") List<String> customerId);
因为您的投影仅返回单个列,firstName
,它可能是一个字符串。
如果没有@Cacheable
,您返回的是代理类(不可序列化)的列表(可序列化)。
使用 @Cacheable 时,列表会被发送到分布式存储 (Hazelcast) 进行缓存,但会失败,因为列表是可序列化的,但列表元素不是。
关于java - HazelcastSerializationException : Failed to serialize Interface-based Projections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54986475/