ClassCastException with PreparedStatemennt upgrading to datastax driver 4.17.0 with java 17 and spring 6 and jetty 12

40 Views Asked by At

Facing the above issue after upgrading application to Java 17 with Spring 6.0.8 and DataStax Java Driver 4.17.0 versions against Apache Cassandra® 5.0-beta1 server.

Caused by: 
java.lang.ClassCastException: class com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement cannot be cast to class com.datastax.oss.driver.api.core.cql.PreparedStatement (com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement and com.datastax.oss.driver.api.core.cql.PreparedStatement are in unnamed module of loader org.eclipse.jetty.webapp.WebAppClassLoader @563e4951)
    at com.infinote.persistence.EntityMetaDataBasedQueryBinder.bindFindByIdQuery(EntityMetaDataBasedQueryBinder.java:161)
    at com.infinote.persistence.dao.CassandraDaoSupport.lambda$get$6(CassandraDaoSupport.java:567)
    at com.infinote.persistence.dao.CassandraDaoSupport.doWithRetry(CassandraDaoSupport.java:648)
    at com.infinote.persistence.dao.CassandraDaoSupport.get(CassandraDaoSupport.java:565)
    at com.infinote.smtp.dao.EmailConfigDao.get(EmailConfigDao.java:20)
    at com.infinote.smtp.EmailServiceImpl.get(EmailServiceImpl.java:156)
    at com.infinote.smtp.EmailServiceImpl.afterPropertiesSet(EmailServiceImpl.java:201)

The Below issue comes when deployoying the code in the server and the table creation step is failing. Below is the code for refence.

CassandraDaoSupport class invoking bindFindByIdQuery method present in EntityMetaDataBasedQueryBinder:

 protected <T> T get(final Class<T> entityType, final Map<String, Object> ids) {
        final EntityMetaData entityMetaData = cassandraConfiguration.getEntityMetaData(entityType);
        List<T> resultSet = doWithRetry((retry) -> {
                BoundStatement boundStatement = new EntityMetaDataBasedQueryBinder(cassandraConfiguration, entityMetaData)
                        .bindFindByIdQuery(ids, retry);
                return find(boundStatement, new EntityMetaDataBasedResultSetMapper<T>(entityMetaData));
        });
        if (resultSet.size() > 1) {
            throw new NonUniqueResultException("Multiple objects found with id " + Arrays.toString(ids.entrySet().toArray()));
        } else if (resultSet.isEmpty()) {
            return null;
        }
        return resultSet.get(0);
    }

EntityMetaDataBasedQueryBinder class bindFindByIdQuery method:

 public BoundStatement bindFindByIdQuery(Map<String, ? extends Object> ids, boolean refreshCache) {
        PreparedStatement statement = (PreparedStatement) SimpleStatement.newInstance(String.valueOf(instance().findByIdStatement(entityMetaData, refreshCache)));
        List<Object> values = new ArrayList<>();
        for (FieldMetaData idField : entityMetaData.idFields()) {
            values.add(ids.get(idField.name()));
        }
        //also need to bind the client id as the last parameter
        if (entityMetaData.supportMultiTenancy()) {
            values.add(cassandraConfiguration.getCurrentClient());
        }
        return statement.bind(values.toArray());
    }

This is present in QueryCache class:

public PreparedStatement findByIdStatement(EntityMetaData metaData, boolean refreshCache) {
        QueryEntry queries = getQueryEntry(metaData);
        if (queries.findById == null || refreshCache) {
            queries.findById = cassandraConfiguration.getSession(metaData).prepare(metaData.findByIdStatement());
        }
        return queries.findById;
    }

Please help me resolve this issue @Cassandra or any one upgraded to 4+ datastax driver version

0

There are 0 best solutions below