java - HazelcastSerializationException : Failed to serialize Interface-based Projections

标签 java spring-data-jpa hazelcast projection

我想缓存基于接口(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/

相关文章:

java - 如何在运行时更新 ThreadPoolExecutor 线程池大小

java - playframework如何分配请求 Action 、反射?

java - HTML 隐藏字段在 Spring Boot Thymeleaf 中未接收模型属性值

java - Hazelcast Spring 集成问题

Java:在按字母顺序排序的文本文件中查找单词的最佳方法

Java:将字符串转换为日期

java - Hazelcast 管理中心配置

java - Hazelcast - 如何在成员处于运行模式期间从 hazelcast 中完全删除 IQueue

java - 将实体的元素添加到同一实体 - @OneToMany

java - 如何在 JPA 中获取外键引用以具有 "ON UPDATE CASCADE ON DELETE CASCADE"功能?