Mapping cassandra list>> field to Java in spring-data-cassandra

后端 未结 1 1677
一向
一向 2021-01-24 08:34

can someone point to me how a field declared list>> can be mapped back into java in spring-data-cassandra. I\'m able to simply sa

1条回答
  •  伪装坚强ぢ
    2021-01-24 09:18

    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>>
    );
    

    And DTO

    public class CTest {
    
        @PrimaryKey
        private int id;
    
        private List> data;
    
        public CTest() {
        }
    
        private void setData(List> data) {
            this.data = data;
        }
    
        public List> 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 results = cassandraOperations.query("SELECT * FROM ctest  WHERE id = 1", new RowMapper() {
    
        private final TypeToken> listOfInt = new TypeToken>() {};
    
        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;
        }
    });
    

    0 讨论(0)
提交回复
热议问题