Skip to content

Commit 84fe504

Browse files
committed
Add java configuration for the JPA sample
Issue #3663
1 parent 327faa8 commit 84fe504

File tree

8 files changed

+291
-80
lines changed

8 files changed

+291
-80
lines changed

spring-batch-samples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ efficient updates to a database table.
109109

110110
[Jdbc Readers and Batch Update sample](./src/main/java/org/springframework/batch/sample/jdbc/README.md)
111111

112+
### JPA Reader and Writer sample
113+
114+
The purpose of this sample is to show to usage of the `JpaPagingItemReader`
115+
and the `JpaItemWriter` to read and write data from/to a database with JPA.
116+
117+
[JPA Reader and Writer sample](./src/main/java/org/springframework/batch/sample/jpa/README.md)
118+
112119
### Amqp Job Sample
113120

114121
This sample shows the use of Spring Batch to write to an `AmqpItemWriter`.

spring-batch-samples/pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<artifactId>spring-jdbc</artifactId>
4343
<version>${spring-framework.version}</version>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-orm</artifactId>
48+
<version>${spring-framework.version}</version>
49+
</dependency>
4550
<dependency>
4651
<groupId>org.springframework</groupId>
4752
<artifactId>spring-context-support</artifactId>
@@ -227,12 +232,6 @@
227232
<version>${spring-framework.version}</version>
228233
<scope>test</scope>
229234
</dependency>
230-
<dependency>
231-
<groupId>org.springframework</groupId>
232-
<artifactId>spring-orm</artifactId>
233-
<version>${spring-framework.version}</version>
234-
<scope>test</scope>
235-
</dependency>
236235
<dependency>
237236
<groupId>jakarta.el</groupId>
238237
<artifactId>jakarta.el-api</artifactId>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2023 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.sample.jpa;
17+
18+
import javax.sql.DataSource;
19+
import jakarta.persistence.EntityManagerFactory;
20+
21+
import org.springframework.batch.core.Job;
22+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
23+
import org.springframework.batch.core.job.builder.JobBuilder;
24+
import org.springframework.batch.core.repository.JobRepository;
25+
import org.springframework.batch.core.step.builder.StepBuilder;
26+
import org.springframework.batch.item.database.JpaItemWriter;
27+
import org.springframework.batch.item.database.JpaPagingItemReader;
28+
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder;
29+
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
30+
import org.springframework.batch.sample.domain.trade.CustomerCredit;
31+
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
35+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
36+
import org.springframework.orm.jpa.JpaTransactionManager;
37+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
38+
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
39+
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
40+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
41+
42+
/**
43+
* Hibernate JPA dialect does not support custom tx isolation levels => overwrite with
44+
* ISOLATION_DEFAULT.
45+
*
46+
* @author Mahmoud Ben Hassine
47+
*/
48+
@Configuration
49+
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT")
50+
public class JpaJobConfiguration {
51+
52+
@Bean
53+
public JpaPagingItemReader<CustomerCredit> itemReader(EntityManagerFactory entityManagerFactory) {
54+
return new JpaPagingItemReaderBuilder<CustomerCredit>().name("itemReader")
55+
.entityManagerFactory(entityManagerFactory)
56+
.queryString("select c from CustomerCredit c")
57+
.build();
58+
}
59+
60+
@Bean
61+
public JpaItemWriter<CustomerCredit> itemWriter(EntityManagerFactory entityManagerFactory) {
62+
return new JpaItemWriterBuilder<CustomerCredit>().entityManagerFactory(entityManagerFactory).build();
63+
}
64+
65+
@Bean
66+
public Job job(JobRepository jobRepository, JpaTransactionManager transactionManager,
67+
JpaPagingItemReader<CustomerCredit> itemReader, JpaItemWriter<CustomerCredit> itemWriter) {
68+
return new JobBuilder("ioSampleJob", jobRepository)
69+
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
70+
.reader(itemReader)
71+
.processor(new CustomerCreditIncreaseProcessor())
72+
.writer(itemWriter)
73+
.build())
74+
.build();
75+
}
76+
77+
// Infrastructure beans
78+
79+
@Bean
80+
public DataSource dataSource() {
81+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
82+
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
83+
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
84+
.addScript("/org/springframework/batch/sample/jpa/sql/schema.sql")
85+
.build();
86+
}
87+
88+
@Bean
89+
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
90+
return new JpaTransactionManager(entityManagerFactory);
91+
}
92+
93+
@Bean
94+
public EntityManagerFactory entityManagerFactory(PersistenceUnitManager persistenceUnitManager,
95+
DataSource dataSource) {
96+
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
97+
factoryBean.setDataSource(dataSource);
98+
factoryBean.setPersistenceUnitManager(persistenceUnitManager);
99+
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
100+
factoryBean.afterPropertiesSet();
101+
return factoryBean.getObject();
102+
}
103+
104+
@Bean
105+
public PersistenceUnitManager persistenceUnitManager(DataSource dataSource) {
106+
DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
107+
persistenceUnitManager.setDefaultDataSource(dataSource);
108+
persistenceUnitManager.afterPropertiesSet();
109+
return persistenceUnitManager;
110+
}
111+
112+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### JPA Reader and Writer sample
2+
3+
## About
4+
5+
The purpose of this sample is to show to usage of the `JpaPagingItemReader`
6+
and the `JpaItemWriter` to read and write data from/to a database with JPA.
7+
8+
## Run the sample
9+
10+
You can run the sample from the command line as following:
11+
12+
```
13+
$>cd spring-batch-samples
14+
# Launch the sample using the XML configuration
15+
$>../mvnw -Dtest=JpaFunctionalTests#testLaunchJobWithXmlConfig test
16+
# Launch the sample using the Java configuration
17+
$>../mvnw -Dtest=JpaFunctionalTests#testLaunchJobWithJavaConfig test
18+
```
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,68 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
5-
6-
<bean id="itemReader"
7-
class="org.springframework.batch.item.database.JpaPagingItemReader">
8-
<property name="entityManagerFactory" ref="entityManagerFactory" />
9-
<property name="queryString" value="select c from CustomerCredit c" />
10-
</bean>
11-
12-
<bean id="itemWriter" class="org.springframework.batch.item.database.JpaItemWriter">
13-
<property name="entityManagerFactory" ref="entityManagerFactory" />
14-
</bean>
15-
16-
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
17-
<property name="entityManagerFactory" ref="entityManagerFactory" />
18-
</bean>
19-
20-
<bean id="entityManagerFactory"
21-
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
22-
<property name="dataSource" ref="dataSource" />
23-
<property name="persistenceUnitName" value="customerCredit" />
24-
<property name="jpaVendorAdapter">
25-
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
26-
<property name="showSql" value="false" />
27-
</bean>
28-
</property>
29-
<property name="jpaDialect">
30-
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
31-
</property>
32-
</bean>
33-
34-
<!--
35-
Hibernate JPA dialect does not support custom tx isolation levels =>
36-
overwrite with ISOLATION_DEFAULT
37-
-->
38-
<bean id="jobRepository"
39-
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
40-
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
41-
<property name="dataSource" ref="dataSource" />
42-
<property name="transactionManager" ref="transactionManager" />
43-
</bean>
44-
</beans>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
5+
xsi:schemaLocation="http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd
6+
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">
7+
8+
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch">
9+
<batch:step id="step1">
10+
<batch:tasklet>
11+
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
12+
commit-interval="2"/>
13+
</batch:tasklet>
14+
</batch:step>
15+
</batch:job>
16+
17+
<bean id="itemProcessor" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
18+
19+
<bean id="itemReader"
20+
class="org.springframework.batch.item.database.JpaPagingItemReader">
21+
<property name="entityManagerFactory" ref="entityManagerFactory" />
22+
<property name="queryString" value="select c from CustomerCredit c" />
23+
</bean>
24+
25+
<bean id="itemWriter" class="org.springframework.batch.item.database.JpaItemWriter">
26+
<property name="entityManagerFactory" ref="entityManagerFactory" />
27+
</bean>
28+
29+
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
30+
<property name="entityManagerFactory" ref="entityManagerFactory" />
31+
</bean>
32+
33+
<bean id="entityManagerFactory"
34+
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
35+
<property name="dataSource" ref="dataSource" />
36+
<property name="persistenceUnitName" value="customerCredit" />
37+
<property name="jpaVendorAdapter">
38+
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
39+
<property name="showSql" value="false" />
40+
</bean>
41+
</property>
42+
<property name="jpaDialect">
43+
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
44+
</property>
45+
</bean>
46+
47+
<!--
48+
Hibernate JPA dialect does not support custom tx isolation levels =>
49+
overwrite with ISOLATION_DEFAULT
50+
-->
51+
<bean id="jobRepository"
52+
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
53+
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
54+
<property name="dataSource" ref="dataSource" />
55+
<property name="transactionManager" ref="transactionManager" />
56+
</bean>
57+
58+
<bean id="jobLauncher"
59+
class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
60+
<property name="jobRepository" ref="jobRepository" />
61+
</bean>
62+
63+
<jdbc:embedded-database id="dataSource" generate-name="true">
64+
<jdbc:script location="org/springframework/batch/core/schema-drop-hsqldb.sql"/>
65+
<jdbc:script location="org/springframework/batch/core/schema-hsqldb.sql"/>
66+
<jdbc:script location="org/springframework/batch/sample/jpa/sql/schema.sql"/>
67+
</jdbc:embedded-database>
68+
</beans>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
DROP TABLE CUSTOMER_SEQ IF EXISTS;
2+
DROP TABLE CUSTOMER IF EXISTS;
3+
4+
CREATE TABLE CUSTOMER_SEQ (
5+
ID BIGINT IDENTITY
6+
);
7+
INSERT INTO CUSTOMER_SEQ (ID) values (5);
8+
9+
CREATE TABLE CUSTOMER (
10+
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
11+
VERSION BIGINT ,
12+
NAME VARCHAR(45) ,
13+
CREDIT DECIMAL(10,2)
14+
) ;
15+
16+
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (1, 0, 'customer1', 100000);
17+
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (2, 0, 'customer2', 100000);
18+
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (3, 0, 'customer3', 100000);
19+
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (4, 0, 'customer4', 100000);

spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JpaFunctionalTests.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2008-2023 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.sample.jpa;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.batch.core.BatchStatus;
21+
import org.springframework.batch.core.Job;
22+
import org.springframework.batch.core.JobExecution;
23+
import org.springframework.batch.core.JobParameters;
24+
import org.springframework.batch.core.launch.JobLauncher;
25+
import org.springframework.batch.test.JobLauncherTestUtils;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.context.ApplicationContext;
28+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
33+
@SpringJUnitConfig(locations = { "/org/springframework/batch/sample/jpa/job/jpa.xml", "/job-runner-context.xml" })
34+
class JpaFunctionalTests {
35+
36+
@Autowired
37+
private JobLauncherTestUtils jobLauncherTestUtils;
38+
39+
@Test
40+
void testLaunchJobWithXmlConfig() throws Exception {
41+
// when
42+
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();
43+
44+
// then
45+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
46+
}
47+
48+
@Test
49+
public void testLaunchJobWithJavaConfig() throws Exception {
50+
// given
51+
ApplicationContext context = new AnnotationConfigApplicationContext(JpaJobConfiguration.class);
52+
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
53+
Job job = context.getBean(Job.class);
54+
55+
// when
56+
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
57+
58+
// then
59+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
60+
}
61+
62+
}

0 commit comments

Comments
 (0)