-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add java configuration for the JPA sample
Issue #3663
- Loading branch information
1 parent
327faa8
commit 84fe504
Showing
8 changed files
with
291 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...batch-samples/src/main/java/org/springframework/batch/sample/jpa/JpaJobConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.sample.jpa; | ||
|
||
import javax.sql.DataSource; | ||
import jakarta.persistence.EntityManagerFactory; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.database.JpaItemWriter; | ||
import org.springframework.batch.item.database.JpaPagingItemReader; | ||
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder; | ||
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder; | ||
import org.springframework.batch.sample.domain.trade.CustomerCredit; | ||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; | ||
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
|
||
/** | ||
* Hibernate JPA dialect does not support custom tx isolation levels => overwrite with | ||
* ISOLATION_DEFAULT. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
*/ | ||
@Configuration | ||
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT") | ||
public class JpaJobConfiguration { | ||
|
||
@Bean | ||
public JpaPagingItemReader<CustomerCredit> itemReader(EntityManagerFactory entityManagerFactory) { | ||
return new JpaPagingItemReaderBuilder<CustomerCredit>().name("itemReader") | ||
.entityManagerFactory(entityManagerFactory) | ||
.queryString("select c from CustomerCredit c") | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JpaItemWriter<CustomerCredit> itemWriter(EntityManagerFactory entityManagerFactory) { | ||
return new JpaItemWriterBuilder<CustomerCredit>().entityManagerFactory(entityManagerFactory).build(); | ||
} | ||
|
||
@Bean | ||
public Job job(JobRepository jobRepository, JpaTransactionManager transactionManager, | ||
JpaPagingItemReader<CustomerCredit> itemReader, JpaItemWriter<CustomerCredit> itemWriter) { | ||
return new JobBuilder("ioSampleJob", jobRepository) | ||
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager) | ||
.reader(itemReader) | ||
.processor(new CustomerCreditIncreaseProcessor()) | ||
.writer(itemWriter) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
// Infrastructure beans | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) | ||
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") | ||
.addScript("/org/springframework/batch/core/schema-hsqldb.sql") | ||
.addScript("/org/springframework/batch/sample/jpa/sql/schema.sql") | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { | ||
return new JpaTransactionManager(entityManagerFactory); | ||
} | ||
|
||
@Bean | ||
public EntityManagerFactory entityManagerFactory(PersistenceUnitManager persistenceUnitManager, | ||
DataSource dataSource) { | ||
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); | ||
factoryBean.setDataSource(dataSource); | ||
factoryBean.setPersistenceUnitManager(persistenceUnitManager); | ||
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); | ||
factoryBean.afterPropertiesSet(); | ||
return factoryBean.getObject(); | ||
} | ||
|
||
@Bean | ||
public PersistenceUnitManager persistenceUnitManager(DataSource dataSource) { | ||
DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); | ||
persistenceUnitManager.setDefaultDataSource(dataSource); | ||
persistenceUnitManager.afterPropertiesSet(); | ||
return persistenceUnitManager; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
spring-batch-samples/src/main/java/org/springframework/batch/sample/jpa/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### JPA Reader and Writer sample | ||
|
||
## About | ||
|
||
The purpose of this sample is to show to usage of the `JpaPagingItemReader` | ||
and the `JpaItemWriter` to read and write data from/to a database with JPA. | ||
|
||
## Run the sample | ||
|
||
You can run the sample from the command line as following: | ||
|
||
``` | ||
$>cd spring-batch-samples | ||
# Launch the sample using the XML configuration | ||
$>../mvnw -Dtest=JpaFunctionalTests#testLaunchJobWithXmlConfig test | ||
# Launch the sample using the Java configuration | ||
$>../mvnw -Dtest=JpaFunctionalTests#testLaunchJobWithJavaConfig test | ||
``` |
112 changes: 68 additions & 44 deletions
112
.../src/main/resources/jobs/iosample/jpa.xml → ...ingframework/batch/sample/jpa/job/jpa.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> | ||
|
||
<bean id="itemReader" | ||
class="org.springframework.batch.item.database.JpaPagingItemReader"> | ||
<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||
<property name="queryString" value="select c from CustomerCredit c" /> | ||
</bean> | ||
|
||
<bean id="itemWriter" class="org.springframework.batch.item.database.JpaItemWriter"> | ||
<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||
</bean> | ||
|
||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | ||
<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||
</bean> | ||
|
||
<bean id="entityManagerFactory" | ||
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | ||
<property name="dataSource" ref="dataSource" /> | ||
<property name="persistenceUnitName" value="customerCredit" /> | ||
<property name="jpaVendorAdapter"> | ||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> | ||
<property name="showSql" value="false" /> | ||
</bean> | ||
</property> | ||
<property name="jpaDialect"> | ||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> | ||
</property> | ||
</bean> | ||
|
||
<!-- | ||
Hibernate JPA dialect does not support custom tx isolation levels => | ||
overwrite with ISOLATION_DEFAULT | ||
--> | ||
<bean id="jobRepository" | ||
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> | ||
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" /> | ||
<property name="dataSource" ref="dataSource" /> | ||
<property name="transactionManager" ref="transactionManager" /> | ||
</bean> | ||
</beans> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc" | ||
xsi:schemaLocation="http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd | ||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> | ||
|
||
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch"> | ||
<batch:step id="step1"> | ||
<batch:tasklet> | ||
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter" | ||
commit-interval="2"/> | ||
</batch:tasklet> | ||
</batch:step> | ||
</batch:job> | ||
|
||
<bean id="itemProcessor" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" /> | ||
|
||
<bean id="itemReader" | ||
class="org.springframework.batch.item.database.JpaPagingItemReader"> | ||
<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||
<property name="queryString" value="select c from CustomerCredit c" /> | ||
</bean> | ||
|
||
<bean id="itemWriter" class="org.springframework.batch.item.database.JpaItemWriter"> | ||
<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||
</bean> | ||
|
||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | ||
<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||
</bean> | ||
|
||
<bean id="entityManagerFactory" | ||
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | ||
<property name="dataSource" ref="dataSource" /> | ||
<property name="persistenceUnitName" value="customerCredit" /> | ||
<property name="jpaVendorAdapter"> | ||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> | ||
<property name="showSql" value="false" /> | ||
</bean> | ||
</property> | ||
<property name="jpaDialect"> | ||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> | ||
</property> | ||
</bean> | ||
|
||
<!-- | ||
Hibernate JPA dialect does not support custom tx isolation levels => | ||
overwrite with ISOLATION_DEFAULT | ||
--> | ||
<bean id="jobRepository" | ||
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> | ||
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" /> | ||
<property name="dataSource" ref="dataSource" /> | ||
<property name="transactionManager" ref="transactionManager" /> | ||
</bean> | ||
|
||
<bean id="jobLauncher" | ||
class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher"> | ||
<property name="jobRepository" ref="jobRepository" /> | ||
</bean> | ||
|
||
<jdbc:embedded-database id="dataSource" generate-name="true"> | ||
<jdbc:script location="org/springframework/batch/core/schema-drop-hsqldb.sql"/> | ||
<jdbc:script location="org/springframework/batch/core/schema-hsqldb.sql"/> | ||
<jdbc:script location="org/springframework/batch/sample/jpa/sql/schema.sql"/> | ||
</jdbc:embedded-database> | ||
</beans> |
19 changes: 19 additions & 0 deletions
19
spring-batch-samples/src/main/resources/org/springframework/batch/sample/jpa/sql/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
DROP TABLE CUSTOMER_SEQ IF EXISTS; | ||
DROP TABLE CUSTOMER IF EXISTS; | ||
|
||
CREATE TABLE CUSTOMER_SEQ ( | ||
ID BIGINT IDENTITY | ||
); | ||
INSERT INTO CUSTOMER_SEQ (ID) values (5); | ||
|
||
CREATE TABLE CUSTOMER ( | ||
ID BIGINT IDENTITY NOT NULL PRIMARY KEY , | ||
VERSION BIGINT , | ||
NAME VARCHAR(45) , | ||
CREDIT DECIMAL(10,2) | ||
) ; | ||
|
||
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (1, 0, 'customer1', 100000); | ||
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (2, 0, 'customer2', 100000); | ||
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (3, 0, 'customer3', 100000); | ||
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (4, 0, 'customer4', 100000); |
30 changes: 0 additions & 30 deletions
30
...h-samples/src/test/java/org/springframework/batch/sample/iosample/JpaFunctionalTests.java
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
...-batch-samples/src/test/java/org/springframework/batch/sample/jpa/JpaFunctionalTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2008-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.sample.jpa; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.batch.core.BatchStatus; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.test.JobLauncherTestUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@SpringJUnitConfig(locations = { "/org/springframework/batch/sample/jpa/job/jpa.xml", "/job-runner-context.xml" }) | ||
class JpaFunctionalTests { | ||
|
||
@Autowired | ||
private JobLauncherTestUtils jobLauncherTestUtils; | ||
|
||
@Test | ||
void testLaunchJobWithXmlConfig() throws Exception { | ||
// when | ||
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(); | ||
|
||
// then | ||
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); | ||
} | ||
|
||
@Test | ||
public void testLaunchJobWithJavaConfig() throws Exception { | ||
// given | ||
ApplicationContext context = new AnnotationConfigApplicationContext(JpaJobConfiguration.class); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
Job job = context.getBean(Job.class); | ||
|
||
// when | ||
JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); | ||
|
||
// then | ||
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); | ||
} | ||
|
||
} |