IN clause with Spring Data and Cassandra @Query

前端 未结 3 1703
执笔经年
执笔经年 2020-12-31 14:08

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

相关标签:
3条回答
  • 2020-12-31 14:14

    It seems to be not possible!

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

    The allowed data types of parameter 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 data types other than these, then org.springframework.data.cassandra.repository.query.CassandraQueryMethod.verify(Method method, RepositoryMetadata metadata) will throw IllegalArgumentException.

    0 讨论(0)
  • 2020-12-31 14:19

    Nitish Bhagat is right, with the current version spring-data-cassandra:2.0.6.RELEASE it does NOT work with the braces.

    For me, it works ONLY without the braces like: @Query("SELECT * FROM people WHERE first_name IN ?0") public List<People> findByFirstName(List<String> firstName);

    0 讨论(0)
  • 2020-12-31 14:32

    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);
    
    0 讨论(0)
提交回复
热议问题