Search code examples
cassandrakundera

Kundera and Cassandra unit issue - String as primary key


I have a kundera model entity which has String as the Id. It is defined like this.

@Id
private String id;

I have a method where I am executing native query like this.

/**
     * @param requestId
     * @return
     */
    public static ProcessRequest getRequest(Serializable requestId) {
        List<ProcessRequest> requests = execute("SELECT * from
            process_requests where id = '" + requestId + "';");
        return requests.isEmpty() ? null : requests.get(0);
    }

I am getting the following exception when running the unit tests like this.

17:50:38.716 [main] ERROR c.i.c.cassandra.CassandraClientBase - Error during executing query SELECT * from process_requests where id = '5b4468e0-6146-4fbc-81cf-571371834446';, Caused by: {} .
com.impetus.kundera.KunderaException: InvalidRequestException(why:Undefined name id in where clause ('id EQ '5b4468e0-6146-4fbc-81cf-571371834446''))
    at com.impetus.client.cassandra.CassandraClientBase.execute(CassandraClientBase.java:2101) [kundera-cassandra-2.14.jar:na]
    at com.impetus.client.cassandra.CassandraClientBase.executeCQLQuery(CassandraClientBase.java:1708) [kundera-cassandra-2.14.jar:na]
    at com.impetus.client.cassandra.CassandraClientBase$CQLClient.executeQuery(CassandraClientBase.java:1851) [kundera-cassandra-2.14.jar:na]
    at com.impetus.client.cassandra.CassandraClientBase.executeSelectQuery(CassandraClientBase.java:761) [kundera-cassandra-2.14.jar:na]
    at com.impetus.client.cassandra.thrift.ThriftClient.executeQuery(ThriftClient.java:860) [kundera-cassandra-2.14.jar:na]
    at com.impetus.client.cassandra.query.CassQuery.populateEntities(CassQuery.java:143) [kundera-cassandra-2.14.jar:na]
    at com.impetus.kundera.query.QueryImpl.fetch(QueryImpl.java:1013) [kundera-core-2.14.jar:na]
    at com.impetus.kundera.query.QueryImpl.getResultList(QueryImpl.java:164) [kundera-core-2.14.jar:na]

Even if there is a column defined by id, it is giving this exception and I have for help exhaustively and could not solve it. Can you please help to solve this?

Update:

CREATE TABLE docyard.process_requests (
id text PRIMARY KEY,
content_type text,
created_at timestamp,
document_id text,
lock bigint,
lock_expiration timestamp,
namespace text,
process_input text,
process_output text,
processed text,
updated_at timestamp,
version_id text
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'min_threshold': '4', 'class':           'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy',       'max_threshold': '32'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';
CREATE INDEX process_requests_lock_idx ON docyard.process_requests  (lock);
CREATE INDEX process_requests_processed_idx ON docyard.process_requests (processed);

Solution

  • If you are using CQL2 for creating your schema, primary key column with any name is created with name 'key'.

    This is handled by Kundera if you are using createQuery or findById. Since you are using native query change where id = ... to where key = ...

    PS: CQL3 is preferred while working with Kundera. You can refer this for more info about CQL3.