PAX
PAX

Reputation: 1156

CassandraRepository with String returning value in query method

I have a Spring application which requests data from a Cassandra data base. Usually, I specify convenient POJOs that I'm using in the query methods of the repository class as data model return result.

But in my current case, I don't want a mapping into a specific model object. I want the query result as raw JSON string (or whatever as Map).

According to following documentation I can request raw JSON by using JSON keyword in the CQL query: https://docs.datastax.com/en/cql/3.3/cql/cql_using/useQueryJSON.html

And theoretically, Spring Data JPA should support simple types as query results: https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-introduction-to-query-methods/

@Repository public interface DataRepository extends CassandraRepository<String> {

  @Query(
     "SELECT JSON uid"
         + ", version"
         + ", timestamp"
         + ", message"
         + " FROM message_data WHERE uid=?0 ALLOW FILTERING")
  Optional<String> findById(UUID id);
}

But finally, I get a mapping error on Spring application's start up:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataController' defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/.../DataController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.String
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.String
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
    ...
Caused by: org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.String
    at org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory.getEntityInformation(CassandraRepositoryFactory.java:104) ~[spring-data-cassandra-1.5.9.RELEASE.jar!/:na]

What am I missing here?

SOLUTION:

According to the accepted answer following code snippet did the trick:

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;

...
@Autowired
private CassandraOperations cassandraTemplate;
...

private Optional<String> findById(final UUID id) {
  final Select select = QueryBuilder.select().json().from("message_data");
  select.where(QueryBuilder.eq(...))
        .and(QueryBuilder.eq("uid", QueryBuilder.raw(id.toString())));

  return Optional.ofNullable(cassandraTemplate.selectOne(select, String.class));
}

private void insert(final MessageEntity entity) {
  cassandraTemplate.insert(entity);
}

Upvotes: 0

Views: 2779

Answers (1)

Shanu Gupta
Shanu Gupta

Reputation: 3807

Your DataRepository is extending CassandraRepository<String> which is a problem.

The CassandraRepository declaration goes like CassandraRepository<T,ID> where

T - the domain type the repository manages
ID - the type of the id of the entity the repository manages

In your case T would be Entity/Table class you are using for message_data table and ID would be UUID.

If you need to run the query without Repository, use cassandraTemplate:

@Autowired
private CassandraOperations cassandraTemplate;

Upvotes: 1

Related Questions