import javax.sql.DataSource;
public final class JpaConfigurations {
private JpaConfigurations() {
}
public static D createDataSource(
DataSourceProperties properties,
Class dataSourceClass
) {
return DataSourceBuilder
.create()
.driverClassName(properties.getDriverClassName())
.url(properties.getJdbcUrl())
.username(properties.getUsername())
.password(properties.getPassword())
.type(dataSourceClass)
.build();
}
public static EntityManagerFactory createEntityManagerFactory(
DataSource dataSource,
HibernateProperties hibernateProperties,
String packageToScan
) {
final LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setPackagesToScan(packageToScan);
factory.setDataSource(dataSource);
factory.setJpaProperties(hibernateProperties);
factory.afterPropertiesSet();
return factory.getObject();
}
public static JpaTransactionManager createJpaTransactionManager(
EntityManagerFactory entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
import javax.sql.DataSource;
@AllArgsConstructor
public class EzyDataSourceFactory {
private final Map dataSourcePropertiesByChainId;
private final Environment environment;
private final String hikariDataSourcePropertyPrefix;
public DataSource createDataSource(String datasourceId) {
final HikariDataSource dataSource = JpaConfigurations.createDataSource(
dataSourcePropertiesByChainId.get(datasourceId),
HikariDataSource.class
);
final Binder binder = Binder.get(environment);
binder.bind(hikariDataSourcePropertyPrefix, Bindable.ofInstance(dataSource));
return dataSource;
}
}
import javax.sql.DataSource;
@AllArgsConstructor
public class EzyEntityManagerFactoryFactory {
private final EzyDataSourceFactory exampleDataSourceFactory;
private final HibernateProperties hibernateProperties;
private final String packageToScan;
public EntityManagerFactory createEntityManagerFactory(String datasourceId) {
return createEntityManagerFactory(datasourceId, null);
}
public EntityManagerFactory createEntityManagerFactory(
String datasourceId,
DataSource dataSource
) {
return JpaConfigurations.createEntityManagerFactory(
dataSource != null ? dataSource : exampleDataSourceFactory.createDataSource(datasourceId),
hibernateProperties,
packageToScan
);
}
}
@AllArgsConstructor
public class EzyJpaRepositoryFactory {
private final EzyJpaTransactionManagerFactory ezyJpaTransactionManagerFactory;
public <T, I, R extends JpaRepository> R createJpaRepository(
String datasourceId,
Class jpaRepositoryClass,
JpaTransactionManager jpaTransactionManager
) {
final JpaTransactionManager actualJpaTransactionManager =
jpaTransactionManager == null
? jpaTransactionManager
: ezyJpaTransactionManagerFactory.createJpaTransactionManager(datasourceId);
final EntityManagerFactory entityManagerFactory =
actualJpaTransactionManager.getEntityManagerFactory();
assert entityManagerFactory != null;
final EntityManager entityManager =
SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
final JpaRepositoryFactory jpaRepositoryFactory = new JpaRepositoryFactory(entityManager);
final TransactionInterceptor transactionInterceptor = new TransactionInterceptor(
(TransactionManager) actualJpaTransactionManager,
new AnnotationTransactionAttributeSource()
);
jpaRepositoryFactory.addRepositoryProxyPostProcessor((factory, repositoryInformation) ->
factory.addAdvice(transactionInterceptor)
);
return jpaRepositoryFactory.getRepository(jpaRepositoryClass);
}
public <T, I, R extends JpaRepository> R createJpaRepositoryWithAspects(
String datasourceId,
Class jpaRepositoryClass,
JpaTransactionManager jpaTransactionManager,
Object... aspects
) {
final R nakedJpaRepository = createJpaRepository(datasourceId, jpaRepositoryClass, jpaTransactionManager);
final AspectJProxyFactory proxyFactory = new AspectJProxyFactory(nakedJpaRepository);
for (Object aspect : aspects) {
proxyFactory.addAspect(aspect);
}
return proxyFactory.getProxy();
}
}
@AllArgsConstructor
public class EzyJpaTransactionManagerFactory {
private final EzyEntityManagerFactoryFactory ezyEntryManagerFactoryFactory;
public JpaTransactionManager createJpaTransactionManager(String datasourceId) {
return createJpaTransactionManager(datasourceId, null);
}
public JpaTransactionManager createJpaTransactionManager(
String datasourceId,
EntityManagerFactory entityManagerFactory
) {
return JpaConfigurations.createJpaTransactionManager(
entityManagerFactory != null
? entityManagerFactory
: ezyEntryManagerFactoryFactory.createEntityManagerFactory(datasourceId)
);
}
}