How to pass to @Query a complete query String?

1.5k views Asked by At

Using the @Query from org.springframework.data.cassandra.repository.Query, I would like to make a custom query and pass it to my Repository and then from the parameter to the @Query annotation.

My Repository looks like that:

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

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


}

Where the String customQuery is the query self. Like " SELECT * FROM TABLE_A WHERE COLUMN_B = C"

I get the following error:

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 '{' ([{]...)

My guess is that the customQuery is a String with quotation marks " ". But I dont know how to pass the whole query like that. Could anyone please help me? :)

1

There are 1 answers

0
Philipp On

JpaRepositories are not designed to do that. If you want to execute a custom JPQL query, you can do that like so:

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