带有Spring Data和Cassandra @Query的IN子句

问题描述 投票:9回答:3

我正在尝试使用IN子句和Spring Data中的@Query注释来查询Cassandra表。我有一个表,其分区键为last_name,聚类键为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。有任何想法吗?

编辑更多信息!

传入数组,设置或列表会返回Caused by: java.lang.IllegalArgumentException: encountered unsupported query parameter type [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);

没有找到,图书馆搜索连接名称qazxsw poi的单个人

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

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

java spring cassandra spring-data spring-data-cassandra
3个回答
10
投票

更新

现在的春天,似乎没有支撑。


老答案

当你使用('Joe'',''Jim')时,你必须使用护腕。

IN

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

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

2
投票

这似乎是不可能的!

我检查了@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-1.3.2.RELEASE.jar

他们可以找到

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

如果我们传递除这些之外的数据类型,那么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 })); 将抛出org.springframework.data.cassandra.repository.query.CassandraQueryMethod.verify(Method method, RepositoryMetadata metadata)


0
投票

Nitish Bhagat是正确的,当前版本是spring-data-cassandra:2.0.6 .RELEASE它不适用于大括号。

对我来说,它只适用于没有像:IllegalArgumentException这样的大括号

© www.soinside.com 2019 - 2024. All rights reserved.