IN clause with Spring Data and Cassandra @Query

Bill H picture Bill H · Apr 2, 2015 · Viewed 11.7k times · Source

I'm trying to query a Cassandra table using the IN clause and the @Query annotation from Spring Data. I have a table with a partition key of last_name and a clustering key of first_name.

I have this query working

@Query("SELECT * FROM people WHERE last_name=?0")
public List<People> findByLastName(String lastName);

and I would like to do something like

@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN ?1")
public List<People> findByLastName(String lastName, String[] firstName);

I have it working using

CassandraOperations.select("SELECT * FROM people WHERE last_name=" + lastName + 
" AND first_name IN (" + concatinatedNameList + ")", People.class);

But for a number of reasons (code style, testing, I swear there are more) I would prefer to use @Query. Any ideas?

EDIT FOR MORE INFO!

Passing in an array, set, or list returns Caused by: java.lang.IllegalArgumentException: encountered unsupported query parameter type [class [Ljava.lang.String;] in method public abstract

Also tried:

String firstName = "Joe,Jim";
@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")
public List<People> findByLastName(String lastName, String firstName);

Nothing found, library searches for a single person with a concatinated name ('Joe,Jim')

String firstName = "'Joe','Jim'";
@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")
public List<People> findByLastName(String lastName, String firstName);

Nothing found, the request is escaped and ends up ('''Joe'',''Jim''')

String firstName = "Joe','Jim"; // Hoping the library would just add the outer quotes, getting desperate
@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")
public List<People> findByLastName(String lastName, String firstName);

Nothing found, the request is escaped and ends up ('Joe'',''Jim')

Answer

Faraj Farook picture Faraj Farook · Apr 3, 2015

Update

With current spring, it seems to be working without braces.


Old answer

You have to use bracers when you are using IN.

@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")
public List<People> findByLastName(String lastName, String[] firstName);

But there are some other issues in your code. I changed them all to a good coding standards as below. Including my personal favorite of using named parameters.

@Query("SELECT p FROM People p WHERE p.lastName = :lastName AND p.firstName IN (:firstNames)")
public List<People> findByName(@Param("lastName") String lastName, @Param("firstNames") String[] firstNames);