这是我的休眠配置:
@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;
}
现在,我想在HQL(使用@Query(value = "query")
)中使用标准分页设置(例如偏移量和限制)进行查询。我知道query.setMaxResults()
和query.setFirstResult()
,但是为此,我需要一个Session
(或者对吗?),但是我没有使用会话来配置Hibernate。
我可以仅使用注释来指定偏移量和查询限制吗?有没有一种方法可以使用HQL以编程方式模拟query.setMaxResults()
和query.setFirstResult()
?
答案 0 :(得分:1)
以下代码可能会对您有所帮助。
public Page<Product> findAll(Pageable pageable){
//custom page
PageRequest customPageable = new PageRequest(pageable.getPageNumber(), 100);
return productRepository.findAll(customPageable);
}