I am configuring my spring boot app to have a second datasource for some reason, the main one which is annotated as @Primary looks like this
@Configuration
@EnableJpaRepositories(basePackages = "com.x.y.z.repository", entityManagerFactoryRef = "primaryEntityManagerFactory", transactionManagerRef = "primaryTransactionManager")
public class PrimaryDataSourceConfig {
@FlywayDataSource
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("primaryDataSource") DataSource dataSource) {
Map<String, Object> properties = new HashMap<>();
/*properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL10Dialect");
properties.put("hibernate.jdbc.lob.non_contextual_creation", true);
properties.put("hibernate.temp.use_jdbc_metadata_defaults", false);
properties.put("hibernate.types.print.banner", false);
properties.put("hibernate.types.db.structure", true);
properties.put("spring.jpa.open-in-view", false);
properties.put("spring.jpa.database-platform", "org.hibernate.dialect.PostgreSQL10Dialect");
properties.put("spring.jpa.properties.javax.persistence.query.timeout", 9000);
properties.put("spring.jpa.properties.hibernate.query.in_clause_parameter_padding", true);*/
return builder
.dataSource(dataSource)
.packages("com.x.y.z.model")
.properties(properties)
.build();
}
@Primary
@Bean(name = "primaryTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("primaryEntityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory.getObject());
}
}
Also I have unittests and cucumber tests(integration tests), unittests run with no problem, but the cucumber tests fail with a message like could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet.
In the stack trace I see that this error aused by: org.postgresql.util.PSQLException: ERROR: relation "mytable" does not exist which actually it really does not exists but my_table exists meaning that FlyWay did its job by creating the database and all tables as expected but JPA is not correctly working its magic which is to map MyTable entity to my_table table which normally before configuring the DataSource manually.
I tried to add the properties in entityManagerFactory function in order to configure EntityManagerFactory. but with those lines uncommented I get another set of errors.
I am not sure if something is missing or the configuration in entityManagerFactory is right or not.
Does anyone know whats wrong and what has to be done ?
-
Spring at it again. JPA specification does not 'automagically' map entity 'MyTable' into anything other than table "MYTABLE". Spring injecting configuration into hibernate though does (but only for Hibernate). Personally, I wouldn't rely on it and specify the table names you want to use in your entities. Otherwise, you can set hibernate's naming (renaming) strategies as outlined here stackoverflow.com/a/25681802/496099Chris– Chris2025年04月07日 14:26:22 +00:00Commented Apr 7 at 14:26