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

azerafati

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??

Alan Hay

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("[email protected]"));
        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());
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best solution for filtering out Elasticsearch results with hate words?

What's best practice in Spring Data JPA Error handling

Spring data JPA many to many retrieve

spring-data-jpa many to many findByID

Spring Data JPA Many to Many with extra column

Spring Data JPA Many to Many save to set

Spring Data Jpa Many to Many Query

Hibernate + Spring Data Jpa: the best way to save entity with one-to-many relationship

What is a store in spring data jpa?

Filtering database rows with spring-data-jpa and spring-mvc

Spring Data JPA - Many columns in where clause

One-To-Many Relationship in Spring Data JPA

One to Many mapping not working Spring data JPA

Spring Data and JPA One to many with MapStruct

Define properties in a custom file, for Spring data JPA

Spring data jpa query to return with multiple properties

Spring Data JPA exclude properties for specific RequestMapping

JPA one-to-many filtering

Filtering in Spring Data JPA Repository using QueryDSL with a nullable foreign key

Error in filtering by child object in Spring Data JPA Query

Spring Data JPA - Using @Query with Many to Many Relationships / Join Table

Spring Data JPA delete from many to many relationship problem

How to implementing a many to many relationship with extra columns in Spring Data JPA?

Spring Data JPA Many to Many with extra column User and Roles

Filtering Json data, no results

What is the Spring Data JPA method for the below query

What is the difference between Hibernate and Spring Data JPA

what did not insert db by spring data jpa

Spring data best practice for preload data that considers application.properties