IN with Spring data and Cassandra @Query

I am trying to query a Cassandra table using an IN clause and @Query annotation from Spring Data. I have a table with the last_name section key and the first_name clustering key.

This request works for me

@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 use

 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 rather use @Query. Any ideas?

EDIT MORE INFO!

Passing to 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, search in the library of one person with a joint 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 runs and ends ('''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 runs and ends ('Joe'',''Jim')

+8
java spring spring-data spring-data-cassandra cassandra
source share
2 answers

When using IN you need to use handcuffs.

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

But there are other problems in your code. I changed them all to good coding standards, as shown 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); 
+9
source share

It seems impossible!

I checked the source code for spring -data-cassandra-1.3.2.RELEASE.jar .

The allowed parameter data types in the query method are String.class, CharSequence.class, char.class, Character.class, char[].class, long.class, Long.class, boolean.class, Boolean.class, BigDecimal.class, BigInteger.class, double.class, Double.class, float.class, Float.class, InetAddress.class, Date.class, UUID.class, int.class are Integer.class .

They can be found in

 org.springframework.data.cassandra.repository.query.CassandraQueryMethod.ALLOWED_PARAMETER_TYPES = Collections.unmodifiableList(Arrays .asList(new Class<?>[] { String.class, CharSequence.class, char.class, Character.class, char[].class, long.class, Long.class, boolean.class, Boolean.class, BigDecimal.class, BigInteger.class, double.class, Double.class, float.class, Float.class, InetAddress.class, Date.class, UUID.class, int.class, Integer.class })); 

If we pass other data types, then org.springframework.data.cassandra.repository.query.CassandraQueryMethod.verify(Method method, RepositoryMetadata metadata) will throw an IllegalArgumentException .

+2
source share

All Articles