IN子句与Spring数据和Cassandra @Query [英] IN clause with Spring Data and Cassandra @Query
问题描述
我正在尝试使用IN子句和来自Spring Data的@Query注释来查询Cassandra表。我有一个表格,有一个分区键last_name和一个聚集键的first_name。
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.
我有这个查询工作
@Query("SELECT * FROM people WHERE last_name=?0")
public List<People> findByLastName(String lastName);
,我想做类似
@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN ?1")
public List<People> findByLastName(String lastName, String[] firstName);
我正在使用
CassandraOperations.select("SELECT * FROM people WHERE last_name=" + lastName +
" AND first_name IN (" + concatinatedNameList + ")", People.class);
但是由于种种原因(代码风格,测试,我发现还有更多)使用@Query。任何想法?
But for a number of reasons (code style, testing, I swear there are more) I would prefer to use @Query. Any ideas?
编辑更多信息!
设置或列表返回导致:java.lang.IllegalArgumentException:遇到不支持的查询参数类型[class [Ljava.lang.String;] in method public abstract
也尝试了:
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);
没有找到任何内容,库会搜索具有连续名称的单个人 Joe,Jim')
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);
未找到任何内容,请求被转义,结束 '',''Jim'')
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);
未找到任何内容,请求被转义,结束('Joe' ,''Jim')
Nothing found, the request is escaped and ends up ('Joe'',''Jim')
推荐答案
$ c> IN 。
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);
这篇关于IN子句与Spring数据和Cassandra @Query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!