spring data cassandra : how to tune the return type of an update @Query

708 views Asked by At

Let’s say I have a table like this

@Table(value = "myTable")
public class MyRow {
    private UUID id;    // primary key
    private String state;
}

I have a query that updates a row based on an existence condition

@Query("update myTable set state='Opened' where id=?0 if exists")
Boolean open(UUID id);

So far, so good, the query returns true if row exists and false otherwise.

Now, I want to change the query to set a condition on current state.

@Query("update myTable set state='Opened' where id=?0 if state='Closed'")
Boolean open(UUID id);

Still, it’s working, the return value reflects if the row has been updated or not. But in case it’s returning false, I’m missing the fact returned by the CQL query about the condition that failed. (explained in https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/cqlUpdate.html#cqlUpdate__conditionally-updating-columns)

Example:

[applied]  | state
-----------|----------- 
     False | Opened

So, instead of Boolean as returned type, I’d like to have something like (or equivalent)

class ConditionalReturn {
  Boolean applied;
  String state;
}

And use instead

@Query("update myTable set state='Opened' where id=?0 if state='Closed'")
ConditionalReturn open(UUID id);

But it does not work… Do you think it’s possible directly with @Query ? [without using the spring cassandra template]

1

There are 1 answers

0
snanduri On

Was in similar situation and March Paluch shared that "LWT return values can only be consumed through CqlTemplate/CassandraTemplate, not repositories."