Generated keys is a mechanism that database generate an automatic value for you, can be auto-generated at column level or a sequence level.
Cassandra doesn’t have SEQUENCE like as Oracle, but you can use UUIDs as Surrogate Key.
Creating table:
CREATE TABLE AUTHOR ( ID text, NAME text, PRIMARY KEY (ID) );
Statement query in xml file:
<statements
xmlns="http://jkniv.sf.net/schema/sqlegance/statements" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jkniv.sf.net/schema/sqlegance/statements http://jkniv.sf.net/schema/sqlegance/sqlegance-stmt.xsd"
>
<insert id="Author#add">
<autoGeneratedKey strategy="SEQUENCE" properties="id">
select uuid() FROM system.local
</autoGeneratedKey>
INSERT INTO AUTHOR (ID, NAME) VALUES (:id, :name)
</insert>
</statements>
How check the results:
Author author = new Author();
author.setName("John Lennon");
int rows = repositoryDb.add(author);
assertThat(rows, is(java.sql.Statement.SUCCESS_NO_INFO));// cassandra driver don't count the number of rows was affected
assertThat(author.getId(), notNullValue());
For compatibility reasons you can use the strategy="AUTO" but doesn’t exist a increment value into Apache Cassandra, the UUIDs mechanism will be use for whinstone-cassandra in this approach too.
<insert id="vechile-autokey-cassandra-case-increment"> <autoGeneratedKey strategy="AUTO" properties="id"/> INSERT INTO AUTHOR (ID, NAME) VALUES (:id, :name) </insert>
Note: the column type must be text or uuid.