For future readers.
I solved this a slightly different way ... using "java config".
The most important part of the below is:
@Bean
@Profile(SPRING_PROFILE_DEFAULT)
public DataSourceInitializer getDataSourceInitializer(final DataSource dataSource) {
That will load the .sql file NOT named "data.sql"....only when that profile is active.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Optional;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
/* below @CS is not needed with setPackagesToScan */
//@ComponentScan(basePackageClasses = {SomeJpaEntityOne.class, SomeJpaEntityTwo.class})
public class PersistenceJpaConfig {
public static final String SPRING_PROFILE_DEFAULT = "default";
/* the below file-name is purposely not "data.sql" (or data-spring.sql) to avoid/bypass "auto-find" spring-data logic. the file/resource is referred to later in a specific spring profile */
@Value("classpath:developer.local.seed.data.dml.sql")
private Resource seedDataLocalDeveloperResource;
/**
* @param env
* @return
*/
/* bean must be named entityManagerFactory to satisfy spring-jpa magic */
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(Environment env) {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(this.getDataSourceOne());
final String entityManagerFactoryPackagesToScanCsv = "com.myentitiespackageone,com.myentitiespackagetwo";
String[] packagesArray = entityManagerFactoryPackagesToScanCsv.split(",");
em.setPackagesToScan(packagesArray);
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(getCustomHibernateProperties(env, configMapRetriever));
return em;
}
@Bean
public DataSource getDataSourceOne() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("myDataSourceDriverClassName");
dataSourceBuilder.url("mydataSourceUrl");
dataSourceBuilder.username("mydataSourceUserName");
dataSourceBuilder.password("myPassword");
DataSource returnItem = dataSourceBuilder.build();
return returnItem;
}
/**
* @param env
* @param secretRetriever
* @param configMapRetriever
* @return JPA PlatformTransactionManager
*/
/* This bean must be named 'transactionManager' to satisfy jpa string-magic */
@Bean(name = "transactionManager")
public PlatformTransactionManager getAPlatformTransactionManager(Environment env) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory(env).getObject());
return transactionManager;
}
/**
* @return JPA PersistenceExceptionTranslationPostProcessor
*/
@Bean
public PersistenceExceptionTranslationPostProcessor getAPersistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties getCustomHibernateProperties(Environment env, IConfigMapRetriever configMapRetriever) {
Properties hibernateProperties = new Properties();
/* not shown */
/* but stuff like
"hibernate.dialect"
"hibernate.hbm2ddl.auto"
"hibernate.jdbc.batch_size"
"hibernate.jdbc.fetch_size"
"hibernate.order_inserts"
"hibernate.order_updates"
"hibernate.jdbc.batch_versioned_data"
"hibernate.generate_statistics"
"hibernate.show_sql"
"hibernate.format_sql"
*/
return hibernateProperties;
}
/**
* @param dataSource
* @return
*/
@Bean
@Profile(SPRING_PROFILE_DEFAULT)
public DataSourceInitializer getDataSourceInitializer(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDatabasePopulator(getDatabasePopulator());
return initializer;
}
private DatabasePopulator getDatabasePopulator() {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(seedDataLocalDeveloperResource);
return populator;
}
}