wenhao / jpa-spec

A JPA Query By Specification framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

findAll with empty query parameter,get total num but no data

kictto opened this issue · comments

     Page<Departments> departmentss = departmentsService.findAll(
                Specifications.<Departments>and()
                        //.eq("deptNo","d001")
                        .build(),
                new PageRequest(1,20)
        );
        if (!CollectionUtils.isEmpty(departmentss.getContent())){
            logger.info("departments num:"+departmentss.getTotalElements()+".");
            List<Departments> list = departmentss.getContent();
            for (Departments dpt : list) {
                logger.info(dpt.toString());
            }
        }else
            logger.error("departments's info is not exist");

but if I use default findAll,all rows will be found.

looking into it right now.

@kictto double checked, seems no issue !

you can clone this project and add follow test to any *Test.java class.

@Test
public void should_be_able_to_find_all_values_by_page() {
    // given
    Person jack = new PersonBuilder()
        .name("Jack")
        .age(18)
        .build();
    Person eric = new PersonBuilder()
        .name("Eric")
        .age(20)
        .build();
    Person jackson = new PersonBuilder()
        .age(30)
        .nickName("Jackson")
        .build();
    personRepository.save(jack);
    personRepository.save(eric);
    personRepository.save(jackson);

    // when
    Page<Person> persons = personRepository.findAll(Specifications.<Person>and().build(), new PageRequest(0, 15));

    // then
    assertThat(persons.getContent().size()).isEqualTo(3);
}

Also, SQL is correct !

Hibernate: 
    select
        person0_.id as id1_2_,
        person0_.age as age2_2_,
        person0_.birthday as birthday3_2_,
        person0_.company as company4_2_,
        person0_.id_card_id as id_card_7_2_,
        person0_.name as name5_2_,
        person0_.nick_name as nick_nam6_2_ 
    from
        person person0_ 
    where
        1=1 limit ?

new PageRequest(1, 20) should be new PageRequest(0, 20) ?

everything is ok after setting new PageRequest(1,20) to new PageRequest(0,20).

thank you!