我正在尝试使用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);
找不到任何内容,图书馆会搜索名称为('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);
未找到任何内容,请求已转义并最终('Joe'',''Jim')
答案 0 :(得分:10)
<强>更新强>
目前的春天,似乎没有支撑。
旧答案
使用IN
时必须使用护腕。
@Query("SELECT * FROM people WHERE last_name=?0 AND first_name IN (?1)")
public List<People> findByLastName(String lastName, String[] firstName);
但您的代码中还存在其他一些问题。我将它们全部改为良好的编码标准,如下所示。包括我个人最喜欢使用命名参数。
@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);
答案 1 :(得分:2)
似乎不可能!
我检查了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)
会抛出IllegalArgumentException
。
答案 2 :(得分:1)
Nitish Bhagat是正确的,当前版本是spring-data-cassandra:2.0.6 .RELEASE NOT 使用大括号。
对我来说,它仅适用于没有大括号,如:
@Query("SELECT * FROM people WHERE first_name IN ?0")
public List<People> findByFirstName(List<String> firstName);