我有一个Spring应用程序,它从Cassandra数据库中请求数据。通常,我将在存储库类的查询方法中使用的方便的POJO指定为数据模型返回结果。
但在我目前的情况下,我不希望映射到特定的模型对象。我希望查询结果为原始JSON字符串(或Map
)。
根据以下文档,我可以在CQL查询中使用JSON
关键字请求原始JSON:Through Associations
理论上,Spring Data JPA应支持简单类型作为查询结果:https://docs.datastax.com/en/cql/3.3/cql/cql_using/useQueryJSON.html
@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);
}
但最后,我在Spring应用程序的启动时遇到映射错误:
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]
我在这里缺少什么?
SOLUTION:
根据接受的答案,下面的代码片段做了诀窍:
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);
}
答案 0 :(得分:1)
DataRepository
正在延长CassandraRepository<String>
这是一个问题。
CassandraRepository
声明就像CassandraRepository<T,ID>
其中
T - the domain type the repository manages
ID - the type of the id of the entity the repository manages
在您的情况下,T
将Entity/Table
用于message_data
表,ID
将为UUID
。
如果您需要运行不带存储库的查询,请使用cassandraTemplate
:
@Autowired
private CassandraOperations cassandraTemplate;