带有 Spring Data 和 Cassandra @Query 的 IN 子句 [英] IN clause with Spring Data and Cassandra @Query

查看:24
本文介绍了带有 Spring Data 和 Cassandra @Query 的 IN 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 IN 子句和 Spring Data 中的 @Query 注释查询 Cassandra 表.我有一个表,分区键为 last_name,聚簇键为 first_name.

我有这个查询工作

@Query("SELECT * FROM people WHERE last_name=?0")公共列表<人物>findByLastName(String lastName);

我想做类似的事情

@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN ?1")公共列表<人物>findByLastName(String lastName, String[] firstName);

我使用它工作

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

但出于多种原因(代码风格、测试,我发誓还有更多)我更喜欢使用@Query.有什么想法吗?

编辑以获得更多信息!

传入数组、集合或列表返回 Caused by: java.lang.IllegalArgumentException: 在方法 public abstract 中遇到不受支持的查询参数类型 [class [Ljava.lang.String;]

也试过了:

String firstName = "Joe,Jim";@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")公共列表<人物>findByLastName(String lastName, String firstName);

没有找到,图书馆搜索一个名字连在一起的人('Joe,Jim')

String firstName = "'Joe','Jim'";@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")公共列表<人物>findByLastName(String lastName, String firstName);

什么都没找到,请求被转义并以('''Joe'',''Jim''')

String firstName = "Joe','Jim";//希望图书馆只添加外部引号,变得绝望@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")公共列表<人物>findByLastName(String lastName, String firstName);

什么都没找到,请求被转义并结束 ('Joe'',''Jim')

解决方案

更新

使用当前的 spring,它似乎可以在没有大括号的情况下工作.


旧答案

在使用IN 时必须使用护腕.

@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")公共列表<人物>findByLastName(String lastName, String[] firstName);

但是您的代码中还有一些其他问题.我将它们全部更改为良好的编码标准,如下所示.包括我个人最喜欢使用命名参数.

@Query("SELECT p FROM People p WHERE p.lastName = :lastName AND p.firstName IN (:firstNames)")公共列表<人物>findByName(@Param("lastName") String lastName, @Param("firstNames") String[] firstNames);

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')

解决方案

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);

这篇关于带有 Spring Data 和 Cassandra @Query 的 IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆