invalidtypeexception:类型类java.util.arraylist的值0与任何cql3类型都不对应

8tntrjer  于 2021-06-10  发布在  Cassandra
关注(0)|答案(1)|浏览(424)

我试图用udt附加冻结列表,但收到以下异常:

org.springframework.data.cassandra.CassandraTypeMismatchException: Query; CQL [UPDATE docs SET items=items+? WHERE ip=?]; Value 0 of type class java.util.ArrayList does not correspond to any CQL3 type; nested exception is com.datastax.driver.core.exceptions.InvalidTypeException: Value 0 of type class java.util.ArrayList does not correspond to any CQL3 type

我这样创建了udt:

CREATE TYPE item (
    type text,
    uuid text,
    timestamp bigint,
    size bigint,
    content text
);

table是这样的:

CREATE TABLE docs (
    ip text PRIMARY KEY,
    items list<frozen<item>>,
    keys list<text>
);

这是我的生日礼物 docs :

@Table("docs")
public class Document implements Serializable {

    private static final long serialVersionUID = 1L;

    @PrimaryKey
    private String ip;
    private List<String> keys;
    private List<Item> items;

    public void setIp(String ip) {
        this.ip = ip;
    }
    public void addNewKey(String key) {
        this.keys.add(key);
    }
    public void addNewItem(Item item) {
        this.items.add(item);
    }

    public Document(String ip) {
        this.ip = ip;
        this.keys = new ArrayList<String>();
        this.items = new ArrayList<Item>();
    }

    public List<Item> getAllItems() {
        return items;
    }
    public int getAllItemsLength() {
        try {
            return items.size();
        } catch (Exception e) {
            return 0;
        }
    }
    public List<String> getAllKeys() {
        return keys;
    }
    public int getAllKeysLength() {
        try {
            return keys.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public String getAsJsonString() {
        return new Gson().toJson(this);
    }
}

以下是udt的观点:

@UserDefinedType(value="item")
public class Item implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotNull
    private String type;
    @NotNull
    private String uuid;
    @NotNull
    private long timestamp;
    @NotNull
    private long size;
    @NotNull
    private String content;

    public Item(String type, String uuid, long timestamp, long size, String content) {
        this.type = type;
        this.uuid = uuid;
        this.timestamp = timestamp;
        this.size = size;
        this.content = content;
    }
}

这里是存储库:

@Query(value="UPDATE docs SET items=items+?0 WHERE ip=?1")
@AllowFiltering
public void updateItemByIp(List<Item> item, String ip);

这里是我调用updateitembyip()的服务:

Item thatItem = new Item(
  "text/plain",
  "hrT4qLrWt1m3vwLU0smlIkwJJS7Y+/KhTudPwVCWf3w=",
  1583782576724,
  26,
  "content"
);
List<Item> itemList = new ArrayList<Item>();
itemList.add(thatItem);

DBRepo.updateItemByIp(itemList, ip); // <-- The exception is triggered here with IDE breakpoints

当我在cqlsh中执行相同的查询时,它工作得非常好。
这是我在这里的第一个问题,希望大家能理解,谢谢大家的帮助:)

tjjdgumg

tjjdgumg1#

解决方案是使用存储库实现的方法: findById() 以及 save() 而不是我自己的方法 updateItemByIp() ,例如:

// inject the repository in the class
@Autowired
private DocumentsRepository DBRepo;

// ...

final Optional<Document> query = DBRepo.findById(923216);
if (query.isPresent()) {

    // create new item
    Item thatItem = new Item(
        "text/plain",
        "hrT4qLrWt1m3vwLU0smlIkwJJS7Y+/KhTudPwVCWf3w=",
        1583782576724,
        26,
        "content"
    );

    // add item to database
    query.map(dbDocument -> {
        dbDocument.addNewItem(thatItem);
        return DBRepo.save(dbDocument);
    });
};

而且效果很好!

相关问题