使用SimpleJdbcInsert插入集合列

时间:2018-11-25 08:04:14

标签: java spring-boot jdbc cassandra

我正在尝试使用org.springframework.jdbc.core.simple.SimpleJdbcInsert类插入这样构建的Cassandra表中:

CREATE TABLE test.profs(id text PRIMARY KEY,
accountid text,
enable boolean,
modifieddate bigint,
name text,
rules set<text>)

当我发送以下请求时:

{
"name": "a",
"rules": [
    "s", "22", 
    "ss", 
    "sfsf"
],
"enable": true,
"accountId": "sss"
}

(它创建一个id,不用担心-已测试)

但是当我尝试插入时,它引发了以下异常: PreparedStatementCallback; SQL [INSERT INTO test.profs (accountId, enable, id, modifiedDate, name, rules) VALUES(?, ?, ?, ?, ?, ?)the column index : 7 is greater than the count of bound variable markers in the CQL: 6; nested exception is java.sql.SQLRecoverableException: the column index : 7 is greater than the count of bound variable markers in the CQL: 6

我认为这与rules列有关,因为当我尝试插入一个元素的数组时,它确实成功了。

编辑:

我尝试使用以下类:

@Data
@AllArgsConstructor
private class CollectionHolder {
    private Collection<?> collection;

    @Override
    public String toString() {
        StringBuilder list = new StringBuilder("{");

        for (Object obj : collection) {
            list.append(obj.toString());
            list.append(',');
        }

        list.delete(list.length()-1, list.length());
        list.append('}');

        return list.toString();
    }
}

它似乎确实成功了,但是现在rules被插入为null。

有帮助吗?

0 个答案:

没有答案