0

This is my hibernate configuration:

@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource,
            final Environment env)
    {
        final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("...");

        final Properties jpaProperties = new Properties();

        jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
        jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
        jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
        jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
        jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));

        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        return entityManagerFactoryBean;
    }

Now I want to make a query in HQL (using @Query(value = "query")) with standard pagination settings like offset and limit. I know about query.setMaxResults() and query.setFirstResult(), but for that I need a Session (or do I?), but I didn't use sessions to configure Hibernate.

Can I use annotations only to specify the offset and limit to queries? Is there a way to use HQL to programmatically simulate query.setMaxResults() and query.setFirstResult()?

4
  • Sprind data manages hibernate session behind the scene. You can get an hibernateTemplate to excecute manual queries if you want but since Spring data provides pagination queries why don't you use it? Commented Jun 27, 2018 at 11:51
  • @akuma8 You mean Page and PageRequest objects? Commented Jun 27, 2018 at 11:59
  • 1
    Yes, is also there the interface PagingAndSortingRepository<T,ID> that your repository can implement to enable paging and sorting. Commented Jun 27, 2018 at 12:07
  • @akuma8 Thanks, will try. Commented Jun 27, 2018 at 12:12

1 Answer 1

1

Following code may help you.

public Page<Product> findAll(Pageable pageable){
    //custom page
    PageRequest customPageable = new PageRequest(pageable.getPageNumber(), 100);
    return productRepository.findAll(customPageable);     
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.