Skip to content

Commit ee91e8a

Browse files
committed
enable spring again
1 parent b17ed01 commit ee91e8a

File tree

4 files changed

+81
-117
lines changed

4 files changed

+81
-117
lines changed

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,6 @@
354354
</exclusion>
355355
</exclusions>
356356
</dependency>
357-
<dependency>
358-
<groupId>org.springframework</groupId>
359-
<artifactId>spring-jdbc</artifactId>
360-
<scope>test</scope>
361-
</dependency>
362357
<dependency>
363358
<groupId>org.springframework</groupId>
364359
<artifactId>spring-orm</artifactId>

src/test/java/com/github/marschall/hibernate/batchsequencegenerator/BatchSequenceGeneratorIntegrationTest.java

Lines changed: 45 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@
22

33
import static org.junit.jupiter.api.Assumptions.assumeTrue;
44

5-
import java.net.URL;
65
import java.sql.Connection;
76
import java.sql.SQLException;
87
import java.util.ArrayList;
9-
import java.util.Collections;
10-
import java.util.HashMap;
118
import java.util.List;
129
import java.util.Map;
1310

1411
import javax.sql.DataSource;
1512

16-
import org.hibernate.cfg.AvailableSettings;
17-
import org.hibernate.jpa.boot.spi.Bootstrap;
1813
import org.junit.jupiter.params.ParameterizedTest;
1914
import org.junit.jupiter.params.provider.Arguments;
2015
import org.junit.jupiter.params.provider.MethodSource;
2116
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2217
import org.springframework.core.env.ConfigurableEnvironment;
2318
import org.springframework.core.env.MapPropertySource;
2419
import org.springframework.core.env.MutablePropertySources;
25-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
2620
import org.springframework.jdbc.datasource.init.DatabasePopulator;
21+
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
2722
import org.springframework.transaction.PlatformTransactionManager;
2823
import org.springframework.transaction.TransactionDefinition;
2924
import org.springframework.transaction.support.DefaultTransactionDefinition;
30-
import org.springframework.transaction.support.TransactionOperations;
3125
import org.springframework.transaction.support.TransactionTemplate;
3226

3327
import com.github.marschall.hibernate.batchsequencegenerator.configurations.FirebirdConfiguration;
@@ -38,18 +32,17 @@
3832
import com.github.marschall.hibernate.batchsequencegenerator.configurations.OracleConfiguration;
3933
import com.github.marschall.hibernate.batchsequencegenerator.configurations.PostgresConfiguration;
4034
import com.github.marschall.hibernate.batchsequencegenerator.configurations.SqlServerConfiguration;
35+
import com.github.marschall.hibernate.batchsequencegenerator.configurations.TransactionManagerConfiguration;
4136
import com.github.marschall.hibernate.batchsequencegenerator.entities.ChildEntity;
4237
import com.github.marschall.hibernate.batchsequencegenerator.entities.ParentEntity;
4338

4439
import jakarta.persistence.EntityManager;
4540
import jakarta.persistence.EntityManagerFactory;
46-
import jakarta.persistence.EntityTransaction;
47-
import jakarta.persistence.spi.PersistenceUnitTransactionType;
4841

4942
public class BatchSequenceGeneratorIntegrationTest {
5043

5144
private AnnotationConfigApplicationContext applicationContext;
52-
private EntityManagerFactory entityManagerFactory;
45+
private TransactionTemplate template;
5346

5447
public static List<Arguments> parameters() {
5548
List<Arguments> parameters = new ArrayList<>();
@@ -78,47 +71,21 @@ private void setUp(Class<?> dataSourceConfiguration, String persistenceUnitName)
7871
assumeTrue(isSupportedOnTravis(persistenceUnitName));
7972
}
8073
this.applicationContext = new AnnotationConfigApplicationContext();
81-
this.applicationContext.register(dataSourceConfiguration);
74+
this.applicationContext.register(dataSourceConfiguration, HibernateConfiguration.class, TransactionManagerConfiguration.class);
8275
ConfigurableEnvironment environment = this.applicationContext.getEnvironment();
8376
MutablePropertySources propertySources = environment.getPropertySources();
84-
Map<String, Object> source = Collections.singletonMap(HibernateConfiguration.PERSISTENCE_UNIT_NAME, persistenceUnitName);
77+
Map<String, Object> source = Map.of(HibernateConfiguration.PERSISTENCE_UNIT_NAME, persistenceUnitName);
8578
propertySources.addFirst(new MapPropertySource("persistence unit name", source));
8679
this.applicationContext.refresh();
8780

88-
URL persistenceXml = BatchSequenceGeneratorIntegrationTest.class.getClassLoader().getResource("META-INF/persistence.xml");
89-
DataSource dataSource = this.applicationContext.getBean(DataSource.class);
90-
91-
Map<String, Object> integrationSettings = new HashMap<>();
92-
integrationSettings.put(AvailableSettings.DATASOURCE, dataSource);
93-
this.entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(persistenceXml, persistenceUnitName, PersistenceUnitTransactionType.RESOURCE_LOCAL, integrationSettings)
94-
.withDataSource(dataSource)
95-
.build();
96-
97-
this.populateDatabase(dataSource);
98-
}
99-
100-
private void populateDatabase(DataSource dataSource) {
101-
// PlatformTransactionManager txManager = this.applicationContext.getBean(PlatformTransactionManager.class);
102-
PlatformTransactionManager txManager = new DataSourceTransactionManager(dataSource);
81+
PlatformTransactionManager txManager = this.applicationContext.getBean(PlatformTransactionManager.class);
10382
TransactionDefinition transactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
104-
TransactionOperations template = new TransactionTemplate(txManager, transactionDefinition);
105-
template.execute(status -> {
106-
return this.populateDatabaseInTransaction();
83+
this.template = new TransactionTemplate(txManager, transactionDefinition);
84+
this.template.execute(status -> {
85+
return this.populateDatabase();
10786
});
10887
}
10988

110-
private Object populateDatabaseInTransaction() {
111-
Map<String, DatabasePopulator> beans = this.applicationContext.getBeansOfType(DatabasePopulator.class);
112-
DataSource dataSource = this.applicationContext.getBean(DataSource.class);
113-
try (Connection connection = dataSource.getConnection()) {
114-
for (DatabasePopulator populator : beans.values()) {
115-
populator.populate(connection);
116-
}
117-
} catch (SQLException e) {
118-
throw new RuntimeException("could initialize database", e);
119-
}
120-
return null;
121-
}
12289

12390

12491
private static boolean isTravis() {
@@ -134,66 +101,57 @@ private static boolean isSupportedOnTravis(String persistenceUnitName) {
134101
|| persistenceUnitName.contains("db2"));
135102
}
136103

104+
private Object populateDatabase() {
105+
Map<String, DatabasePopulator> beans = this.applicationContext.getBeansOfType(DatabasePopulator.class);
106+
DataSource dataSource = this.applicationContext.getBean(DataSource.class);
107+
try (Connection connection = dataSource.getConnection()) {
108+
for (DatabasePopulator populator : beans.values()) {
109+
populator.populate(connection);
110+
}
111+
} catch (SQLException e) {
112+
throw new RuntimeException("could initialize database", e);
113+
}
114+
return null;
115+
}
116+
137117
private void tearDown() {
138118
if (this.applicationContext == null) { // unsupported database on travis
139119
return;
140120
}
141-
this.entityManagerFactory.close();
142121
this.applicationContext.close();
143122
}
144123

145124
@ParameterizedTest
146125
@MethodSource("parameters")
147126
public void parentChildInstert(Class<?> dataSourceConfiguration, String persistenceUnitName) {
148127
this.setUp(dataSourceConfiguration, persistenceUnitName);
128+
EntityManagerFactory factory = this.applicationContext.getBean(EntityManagerFactory.class);
149129
try {
150-
EntityManager entityManager = this.entityManagerFactory.createEntityManager();
151-
try {
152-
this.runInTransaction(entityManager, () -> this.verifyParentChildInstert(entityManager));
153-
} finally {
154-
entityManager.close();
155-
}
130+
this.template.execute(status -> {
131+
EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
132+
int parentCount = 100;
133+
List<ParentEntity> parents = new ArrayList<>(parentCount);
134+
for (int i = 0; i < parentCount; i++) {
135+
ParentEntity parent = new ParentEntity();
136+
137+
parent.addChild(new ChildEntity());
138+
parent.addChild(new ChildEntity());
139+
140+
parents.add(parent);
141+
}
142+
for (ParentEntity parent : parents) {
143+
entityManager.persist(parent);
144+
for (ChildEntity child : parent.getChildren()) {
145+
child.setParentId(parent.getParentId());
146+
entityManager.persist(child);
147+
}
148+
}
149+
status.flush();
150+
return null;
151+
});
156152
} finally {
157153
this.tearDown();
158154
}
159155
}
160156

161-
private void runInTransaction(EntityManager entityManager, Runnable callback) {
162-
EntityTransaction transaction = entityManager.getTransaction();
163-
transaction.begin();
164-
try {
165-
callback.run();
166-
if (transaction.getRollbackOnly()) {
167-
transaction.rollback();
168-
} else {
169-
transaction.commit();
170-
}
171-
} catch (Throwable t) {
172-
if (transaction.isActive()) {
173-
transaction.rollback();
174-
}
175-
throw t;
176-
}
177-
}
178-
179-
private void verifyParentChildInstert(EntityManager entityManager) {
180-
int parentCount = 100;
181-
List<ParentEntity> parents = new ArrayList<>(parentCount);
182-
for (int i = 0; i < parentCount; i++) {
183-
ParentEntity parent = new ParentEntity();
184-
185-
parent.addChild(new ChildEntity());
186-
parent.addChild(new ChildEntity());
187-
188-
parents.add(parent);
189-
}
190-
for (ParentEntity parent : parents) {
191-
entityManager.persist(parent);
192-
for (ChildEntity child : parent.getChildren()) {
193-
child.setParentId(parent.getParentId());
194-
entityManager.persist(child);
195-
}
196-
}
197-
}
198-
199157
}

src/test/java/com/github/marschall/hibernate/batchsequencegenerator/configurations/HibernateConfiguration.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import javax.sql.DataSource;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
78
import org.springframework.core.env.Environment;
9+
import org.springframework.orm.jpa.JpaDialect;
10+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
11+
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
12+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
813

914
@Configuration
1015
public class HibernateConfiguration {
@@ -18,19 +23,19 @@ public class HibernateConfiguration {
1823
private DataSource dataSource;
1924

2025

21-
// @Bean
22-
// public LocalContainerEntityManagerFactoryBean entityManager() {
23-
// LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
24-
// bean.setPersistenceUnitName(environment.getProperty(PERSISTENCE_UNIT_NAME));
25-
// bean.setJpaDialect(jpaDialect());
26-
// bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
27-
// bean.setDataSource(dataSource);
28-
// return bean;
29-
// }
30-
//
31-
// @Bean
32-
// public JpaDialect jpaDialect() {
33-
// return new HibernateJpaDialect();
34-
// }
26+
@Bean
27+
public LocalContainerEntityManagerFactoryBean entityManager() {
28+
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
29+
bean.setPersistenceUnitName(this.environment.getProperty(PERSISTENCE_UNIT_NAME));
30+
bean.setJpaDialect(this.jpaDialect());
31+
bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
32+
bean.setDataSource(this.dataSource);
33+
return bean;
34+
}
35+
36+
@Bean
37+
public JpaDialect jpaDialect() {
38+
return new HibernateJpaDialect();
39+
}
3540

3641
}

src/test/java/com/github/marschall/hibernate/batchsequencegenerator/configurations/TransactionManagerConfiguration.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import javax.sql.DataSource;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
78
import org.springframework.core.env.Environment;
9+
import org.springframework.orm.jpa.JpaDialect;
10+
import org.springframework.orm.jpa.JpaTransactionManager;
11+
import org.springframework.transaction.PlatformTransactionManager;
12+
13+
import jakarta.persistence.EntityManagerFactory;
814

915
@Configuration
1016
public class TransactionManagerConfiguration {
@@ -15,16 +21,16 @@ public class TransactionManagerConfiguration {
1521
@Autowired
1622
private DataSource dataSource;
1723

18-
// @Autowired
19-
// private JpaDialect jpaDialect;
20-
21-
// @Bean
22-
// public PlatformTransactionManager txManager(EntityManagerFactory emf) {
23-
// JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
24-
// transactionManager.setDataSource(this.dataSource);
25-
// transactionManager.setJpaDialect(this.jpaDialect);
26-
// transactionManager.setPersistenceUnitName(environment.getProperty(HibernateConfiguration.PERSISTENCE_UNIT_NAME));
27-
// return transactionManager;
28-
// }
24+
@Autowired
25+
private JpaDialect jpaDialect;
26+
27+
@Bean
28+
public PlatformTransactionManager txManager(EntityManagerFactory emf) {
29+
JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
30+
transactionManager.setDataSource(this.dataSource);
31+
transactionManager.setJpaDialect(this.jpaDialect);
32+
transactionManager.setPersistenceUnitName(this.environment.getProperty(HibernateConfiguration.PERSISTENCE_UNIT_NAME));
33+
return transactionManager;
34+
}
2935

3036
}

0 commit comments

Comments
 (0)