gpt4 book ai didi

java - CassandraRepository 在查询方法中使用字符串返回值

转载 作者:行者123 更新时间:2023-12-02 11:13:17 25 4
gpt4 key购买 nike

我有一个 Spring 应用程序,它从 Cassandra 数据库请求数据。通常,我会指定在存储库类的查询方法中使用的方便的 POJO 作为数据模型返回结果。

但在我当前的情况下,我不希望映射到特定的模型对象。我希望查询结果为原始 JSON 字符串(或 Map 等)。

根据以下文档,我可以通过在 CQL 查询中使用 JSON 关键字来请求原始 JSON:https://docs.datastax.com/en/cql/3.3/cql/cql_using/useQueryJSON.html

理论上,Spring Data JPA应该支持简单类型作为查询结果: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);
}

但最后,我在 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]

我在这里缺少什么?

解决方案:

根据接受的答案,以下代码片段成功了:

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);
}

最佳答案

您的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;

关于java - CassandraRepository 在查询方法中使用字符串返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com