PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderNativeQueryIntegrationTests.java

http://github.com/SpringSource/spring-batch
Java | 109 lines | 73 code | 16 blank | 20 comment | 0 complexity | 7899818b7e41bb7b32a850cca52a629a MD5 | raw file
  1. /*
  2. * Copyright 2010-2021 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.batch.item.database;
  17. import java.util.Collections;
  18. import javax.persistence.EntityManagerFactory;
  19. import javax.sql.DataSource;
  20. import org.junit.runner.RunWith;
  21. import org.springframework.batch.item.database.orm.JpaNativeQueryProvider;
  22. import org.springframework.batch.item.sample.Foo;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.context.annotation.Bean;
  25. import org.springframework.context.annotation.Configuration;
  26. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  27. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
  28. import org.springframework.orm.jpa.JpaTransactionManager;
  29. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  30. import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
  31. import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
  32. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  33. import org.springframework.test.context.ContextConfiguration;
  34. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  35. import org.springframework.transaction.PlatformTransactionManager;
  36. /**
  37. * @author Anatoly Polinsky
  38. * @author Dave Syer
  39. */
  40. @RunWith(SpringJUnit4ClassRunner.class)
  41. @ContextConfiguration(classes = JpaPagingItemReaderNativeQueryIntegrationTests.JpaConfiguration.class)
  42. public class JpaPagingItemReaderNativeQueryIntegrationTests extends AbstractPagingItemReaderParameterTests {
  43. @Autowired
  44. private EntityManagerFactory entityManagerFactory;
  45. @Override
  46. protected AbstractPagingItemReader<Foo> getItemReader() throws Exception {
  47. String sqlQuery = "select * from T_FOOS where value >= :limit";
  48. JpaPagingItemReader<Foo> reader = new JpaPagingItemReader<>();
  49. //creating a native query provider as it would be created in configuration
  50. JpaNativeQueryProvider<Foo> queryProvider= new JpaNativeQueryProvider<>();
  51. queryProvider.setSqlQuery(sqlQuery);
  52. queryProvider.setEntityClass(Foo.class);
  53. queryProvider.afterPropertiesSet();
  54. reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 2));
  55. reader.setEntityManagerFactory(entityManagerFactory);
  56. reader.setPageSize(3);
  57. reader.setQueryProvider(queryProvider);
  58. reader.afterPropertiesSet();
  59. reader.setSaveState(true);
  60. return reader;
  61. }
  62. @Configuration
  63. public static class JpaConfiguration {
  64. @Bean
  65. public DataSource dataSource() {
  66. return new EmbeddedDatabaseBuilder()
  67. .setType(EmbeddedDatabaseType.HSQL)
  68. .addScript("org/springframework/batch/item/database/init-foo-schema-hsqldb.sql")
  69. .generateUniqueName(true)
  70. .build();
  71. }
  72. @Bean
  73. public PersistenceUnitManager persistenceUnitManager() {
  74. DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
  75. persistenceUnitManager.setDefaultDataSource(dataSource());
  76. persistenceUnitManager.afterPropertiesSet();
  77. return persistenceUnitManager;
  78. }
  79. @Bean
  80. public EntityManagerFactory entityManagerFactory() {
  81. LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  82. factoryBean.setDataSource(dataSource());
  83. factoryBean.setPersistenceUnitManager(persistenceUnitManager());
  84. factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  85. factoryBean.afterPropertiesSet();
  86. return factoryBean.getObject();
  87. }
  88. @Bean
  89. public PlatformTransactionManager transactionManager() {
  90. return new JpaTransactionManager(entityManagerFactory());
  91. }
  92. }
  93. }