gpt4 book ai didi

java - 如何将完整的查询字符串传递给@Query?

转载 作者:太空宇宙 更新时间:2023-11-04 09:05:14 25 4
gpt4 key购买 nike

使用 org.springframework.data.cassandra.repository.Query 中的 @Query,我想创建一个自定义查询并将其传递到我的存储库,然后从参数传递到 @Query 注释。

我的存储库看起来像这样:

@Repository
public interface TestRepository extends CassandraRepository<TestValues, id> {

@Query(value = ":customQuery")
public List<TestValues> testSearch(@Param("customQuery") String customQuery);


}

其中 String customQuery 是查询自身。就像“SELECT * FROM TABLE_A WHERE COLUMN_B = C”

我收到以下错误:

ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.cassandra.CassandraQuerySyntaxException: Query; CQL [{{customQuery}}]; line 1:0 no viable alternative at input '{' ([{]...); nested exception is com.datastax.driver.core.exceptions.SyntaxError: line 1:0 no viable alternative at input '{' ([{]...)] with root cause
com.datastax.driver.core.exceptions.SyntaxError: line 1:0 no viable alternative at input '{' ([{]...)

我的猜测是,customQuery 是一个带引号“”的字符串。但我不知道如何像这样传递整个查询。有人可以帮我吗? :)

最佳答案

JpaRepositories 并不是为此而设计的。如果您想执行自定义 JPQL 查询,您可以这样做:

@PersistenceUnit
private EntityManagerFactory emf;
EntityManager em = emf.createEntityManager();
String customQuery = "SELECT t FROM TestValues t";
List<TestValues> testValues = em.createQuery(customQuery).getResultList();

关于java - 如何将完整的查询字符串传递给@Query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60283509/

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