Search code examples
scalaignitepojo

Scala case class constructor error when used as POJO strategy in Ignite persistence


I have a case class in Scala as so:

class StateCache(greeting: String, first_name: String, last_name: String)

I am using Spark + Ignite to have a write through cache that persists to Cassandra. When setting the persistence-settings.xml file to:

<persistence keyspace="testing_ignite" table="people_test">
    <keyPersistence class="java.lang.Long" strategy="PRIMITIVE" column="index"/>
    <valuePersistence class="StateCache" strategy="POJO"/>
</persistence>

I receive the following error: Java class 'StateCache' couldn't be used as POJO cause it doesn't have no arguments constructor. I have added in an empty constructor but it does not resolve the error. Any help would be much appreciated.


Solution

  • According to the documentation for POJO (highlighting is mine):

    Stores each field of an object as a column having corresponding type in Cassandra table. Provides ability to utilize Cassandra secondary indexes for object fields. Could be used only for POJO objects following Java Beans convention and having their fields of simple java type which could be directly mapped to corresponding Cassandra types.

    Your class StateCache does not follow Java Beans spec. To fix this you need:

    The code would go like this:

    class StateCache(@BeanProperty var greeting: String, 
                     @BeanProperty var first_name: String, 
                     @BeanProperty var last_name: String) {
        def this() {
            this(null,null,null);
        }
    }
    

    This is not an idiomatic Scala but this is what you need to interact with the world that uses Java conventions.