How can I create a Statement
using Spring Data Cassandra
in a way that spring would handle type conversion as well?
If I have a simple query with simple types, I could write something like this:
SimpleStatement.newInstance("UPDATE User SET name = ? WHERE id = ?", "Soheil", 1);
But when I have a UserDefinedType
and I want to create a more complex query, Say table User
has a field pictures
with type of List<frozen<MyPicture>>
, and I want to append to this list using this query:
List<MyPicture> pictures = ...;
SimpleStatement.newInstance("UPDATE User SET profilePictures = profilePictures + ? WHERE id = ?", pictures, userId);
I get a error complaining it cant bind pictures
parameter correctly.
Codec not found for requested operation: [null <-> path.to.package.MyPicture]
As I had already tested this query using CassandraRepository
and @Query
and succeeded, I noticed that somewhere along the way Spring
uses MappingCassandraConverter
to convert List<MyPicture>
into something usable. So I attempted to @Autowire
a instance of the converter and converted my List<MyPicture>
:
List<MyPicture> pictures = ...;
Object converted = mappingCassandraConverter.convertToColumnType(pictures);
SimpleStatement.newInstance("UPDATE User SET profilePictures = profilePictures + ? WHERE id = ?", converted, userId);
And now it works fine with no error. I was wondering if there's a straightforward way to create a Statement
without having to manually inject converter and converting fields?