java.lang.classcastexception:java.util.arraylist不能用cassandra转换为java.util.uuid异常?

lfapxunr  于 2021-06-13  发布在  Cassandra
关注(0)|答案(1)|浏览(484)

我有一个springbootjava应用程序,它可以与cassandra通信。但是我的一个查询失败了。

public class ParameterisedListItemRepository {

        private PreparedStatement findByIds;

        public ParameterisedListItemRepository(Session session, Validator validator, ParameterisedListMsisdnRepository parameterisedListMsisdnRepository ) {
            this.findByIds =  session.prepare("SELECT * FROM mep_parameterisedListItem WHERE id IN ( :ids )");

    }
    public List<ParameterisedListItem> findAll(List<UUID> ids){

        List<ParameterisedListItem> parameterisedListItemList = new ArrayList<>();

        BoundStatement stmt =this.findByIds.bind();
        stmt.setList("ids", ids);
        session.execute(stmt)
            .all()
            .stream()
            .map(parameterisedListItemMapper)
            .forEach(parameterisedListItemList::add);
        return parameterisedListItemList;
    }
    }

下面是堆栈跟踪

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.UUID
    at com.datastax.driver.core.TypeCodec$AbstractUUIDCodec.serialize(TypeCodec.java:1626)
    at com.datastax.driver.core.AbstractData.setList(AbstractData.java:358)
    at com.datastax.driver.core.AbstractData.setList(AbstractData.java:374)
    at com.datastax.driver.core.BoundStatement.setList(BoundStatement.java:681)
    at com.openmind.primecast.repository.ParameterisedListItemRepository.findAll(ParameterisedListItemRepository.java:128)
    at com.openmind.primecast.repository.ParameterisedListItemRepository$$FastClassBySpringCGLIB$$46ffc15e.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at com.openmind.primecast.repository.ParameterisedListItemRepository$$EnhancerBySpringCGLIB$$b2db3c41.findAll(<generated>)
    at com.openmind.primecast.service.impl.ParameterisedListItemServiceImpl.findByParameterisedList(ParameterisedListItemServiceImpl.java:102)
    at com.openmind.primecast.web.rest.ParameterisedListItemResource.getParameterisedListItemsByParameterisedList(ParameterisedListItemResource.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

你知道怎么回事吗。我知道这个问题

SELECT * FROM mep_parameterisedListItem WHERE id IN ( :ids )

知道如何更改findall函数来实现查询吗?
这是表定义

CREATE TABLE "Openmind".mep_parameterisedlistitem (
    id uuid PRIMARY KEY,
    data text,
    msisdn text,
    ordernumber int,
    parameterisedlist uuid
) WITH COMPACT STORAGE;

谢谢您。

wwtsj6pe

wwtsj6pe1#

在不知道表模式的情况下,我的猜测是对表进行了更改,因此该模式不再与prepared语句中的绑定匹配。
问题的很大一部分是你对 SELECT * . 我们的最佳实践建议是显式命名从表中检索的所有列。通过在查询中指定列,可以避免表架构更改时出现意外情况。
在本例中,要么添加了新列,要么删除了旧列。对于缓存的prepared语句,它需要一种列类型,而得到另一种列类型 ArrayList 不匹配 UUID .
解决方案是重新准备语句并命名所有列。干杯!

相关问题