What is the best solution for filtering results in Spring Data JPA with many properties?

孤人 提交于 2019-11-26 22:27:07

问题


I have a case here for a products Table, That needs to be filtered by user input like Category, color, size, price range and many more.

I'm using Spring Data JPA and happy with its derived query from method name and when I'm forced to I just use the @query option for more complex queries like joins and ...

But for the Filter method that I need, I'm afraid I have to write something like this

public interface ProductRepository extends JpaRepository<Product, Long> {
   //..... other methods

Page<Product> findByCategoriesContainingAndSalepriceBetween(List<Category> categories, Float minprice, Float maxprice, PageRequest pagerequest);

Page<Product> findByCategoriesContaining(List<Category> categories, PageRequest pagerequest);

Page<Product> findByCategoriesContainingAndSizeIn(List<Category> categories,Int[] sizes, PageRequest pagerequest);

Page<Product> findByCategoriesContainingAndSizeInAndSalepriceBetween(List<Category> categories,Float minprice, Float maxprice, PageRequest pagerequest);

}

it seems adding some other fields will force me to write so many different combinations

so I've looked at QueryDsl and Specification but they seem so much of extra code, could you put me on the right path??


回答1:


I would disagree that QueryDSL results in a lot of extra code. Here's some test code I have for QueryDSL:

Repository Definition:

public interface UserRepository extends PagingAndSortingRepository<User, Long>, QueryDslPredicateExecutor<User> {

    public User findOne(Predicate predicate);

    public List<User> findAll(Predicate predicate);
}

Test Code based on various attributes and combinations thereof:

 @Test
    public void testFindByEmailAddress() {
        User user = repository.findOne(QUser.user.emailAddress.eq("jack@hamilton.net"));
        Assert.assertNotNull(user);
        Assert.assertEquals("Jack", user.getForename());
        Assert.assertEquals("Hamilton", user.getSurname());
    }

    @Test
    public void testFindByGender() {
        List<User> users = repository.findAll(QUser.user.gender.eq(Gender.M));
        Assert.assertEquals(4, users.size());

        users = repository.findAll(QUser.user.gender.eq(Gender.F));
        Assert.assertEquals(2, users.size());
    }

    @Test
    public void testFindByCity() {

        List<User> users = repository.findAll(QUser.user.address.town.eq("Edinburgh"));
        Assert.assertEquals(2, users.size());

        users = repository.findAll(QUser.user.address.town.eq("Stirling"));
        Assert.assertEquals(1, users.size());
    }

    @Test
    public void testFindByGenderAndCity() {
        List<User> users = repository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.M)));
        Assert.assertEquals(2, users.size());

        users = repository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.F)));
        Assert.assertEquals(1, users.size());
    }


来源:https://stackoverflow.com/questions/26449410/what-is-the-best-solution-for-filtering-results-in-spring-data-jpa-with-many-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!