Search code examples
javaignitegridgain

how to resolve ignitecheckedexception: make sure all objects in cache configuration are serializable


I am running Ignite with Spring Boot.My intention is to perform SqlFieldQuery over BinaryObject in Ignite mentioning that I dont have any entity class, creating entity at runtime by using QueryEntity. This is my below code:

 CacheConfiguration<Integer, BinaryObject> cachecfg = new CacheConfiguration<>();   
 cachecfg.setQueryEntities(new ArrayList<QueryEntity>() 
{
 {

            QueryEntity e = new QueryEntity();
            e.setKeyType("java.lang.Integer");
            e.setValueType("BinaryTest");
            e.setFields(new LinkedHashMap<String, String>() {
                {
                    put("name", "java.lang.String");
                }
            });
            add(e);
        }
    });
    cachecfg.setName("MY_CACHE");
    IgniteCache<Integer, BinaryObject> ignitecache = igniteInstance.createCache(cachecfg).withKeepBinary();
    BinaryObjectBuilder builder = igniteInstance.binary().builder("BinaryTest");
    builder.setField("name", "Test");
    ignitecache.put(1, builder.build());
    QueryCursor<List<?>> query = ignitecache.query(new SqlFieldsQuery("select name from BinaryTest"));
    System.out.println(query.getAll());

when I am running this code, its getting Error:

class org.apache.ignite.IgniteCheckedException: Failed to validate cache 
configuration (make sure all objects in cache configuration are 
serializable): MY_CACHE

I might be missing some configuration for serialization.Is there anything wrong?


Solution

  • Get rid of double brace initialization, in this case you end up with a anonymous class that is a subclass of an ArrayList which has an instance initializer. Anonymous classes always contain reference to the enclosing class, and in your case it's trying to be serialized.

    So go with:

        CacheConfiguration<Integer, BinaryObject> cachecfg = new CacheConfiguration<>();
    
        QueryEntity e = new QueryEntity();
        e.setKeyType("java.lang.Integer");
        e.setValueType("BinaryTest");
    
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("name", "java.lang.String");
        e.setFields(map);
    
        ArrayList<QueryEntity> list = new ArrayList<QueryEntity>();
        list.add(e);
        cachecfg.setQueryEntities(list);
        cachecfg.setName("MY_CACHE");
        IgniteCache<Integer, BinaryObject> ignitecache = igniteInstance.createCache(cachecfg).withKeepBinary();