Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

How to Query against a List field and return a Page with Spring Data Hazelcast?

I have a Spring Boot project using spring-data-hazelcast 2.4.0. It is a really nice library providing HazelcastRepository which abstracts hazelcast interactions behind the standard Spring Data Repository paradigm. But I've run into the below problem when trying to do a pageable query against a list field in my object:

I have this object:

@Data
@KeySpace
public class Book implements Serializable {

    @Id
    private Long id;
    private String author;
    private List<String> categories;

}

I'd like to perform a Pageable query against the list field using HazelcastRepository. I can query within the list using the org.springframework.data.hazelcast.repository.query.Query annotation below and it works fine:

@Query("categories[any] = %s")
Iterable<Book> findByCategory(String category);

But when I try to return a Page with this method:

@Query("categories[any] = %s")
Page<Book> findByCategory(String category, Pageable pageable);

I get the below exception: java.lang.ClassCastException: com.hazelcast.map.impl.query.QueryResultCollection incompatible with org.springframework.data.domain.Page

As an alternative, I've tried just using a derived query method like:

Page<Book> findByCategories(String category, Pageable pageable);

But that throws this exception: java.lang.IllegalArgumentException: Cannot use EqualPredicate predicate with an array or a collection attribute

Is there a way to combine searching within a list property and returning a Page result with HazelcastRepository?

question from:https://stackoverflow.com/questions/65883772/how-to-query-against-a-list-field-and-return-a-page-with-spring-data-hazelcast

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think that you can use @Query with Page<> in Hazelcast. However, your second example (Page<Book> findByCategories(String category, Pageable pageable);) should work correctly.

To get it working, please check samples in the tests:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...