【问题标题】:How to pass to @Query a complete query String?如何将完整的查询字符串传递给@Query?
【发布时间】:2020-06-02 15:05:04
【问题描述】:

使用来自 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);


}

其中字符串 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 是一个带引号“”的字符串。但我不知道如何像这样传递整个查询。有人可以帮我吗? :)

【问题讨论】:

    标签: java cassandra spring-data cql3


    【解决方案1】:

    JpaRepositories 的设计初衷并非如此。如果要执行自定义 JPQL 查询,可以这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2019-02-15
      相关资源
      最近更新 更多