Mapping cassandra list<frozen<list<int>>> field to Java in spring-data-cassandra

拈花ヽ惹草 提交于 2020-01-05 06:28:36

问题


can someone point to me how a field declared list<frozen<list<int>>> can be mapped back into java in spring-data-cassandra. I'm able to simply save data through List<List<Integer>>> but doesn't work when reading from the database, a codec not found exception pops.

Help is much appreciated.


回答1:


Your Declaration is correct. But for nested collection read you need to create Custom RowMapper to convert row to DTO.

Example :

Let's we have the table ctest

CREATE TABLE ctest (
    id int PRIMARY KEY,
    data list<frozen<list<int>>>
);

And DTO

public class CTest {

    @PrimaryKey
    private int id;

    private List<List<Integer>> data;

    public CTest() {
    }

    private void setData(List<List<Integer>> data) {
        this.data = data;
    }

    public List<List<Integer>> getData() {
        return data;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

}

Now we want to query data from it.

List<CTest> results = cassandraOperations.query("SELECT * FROM ctest  WHERE id = 1", new RowMapper<CTest>() {

    private final TypeToken<List<Integer>> listOfInt = new TypeToken<List<Integer>>() {};

    public CTest mapRow(Row row, int rowNum) throws DriverException {
        CTest test = new CTest();
        test.setId(row.getInt("id"));
        test.setData(row.getList("data", listOfInt));
        return test;
    }
});


来源:https://stackoverflow.com/questions/43199198/mapping-cassandra-listfrozenlistint-field-to-java-in-spring-data-cassandra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!