|
2 | 2 |
|
3 | 3 | import ca.uhn.fhir.context.ConfigurationException;
|
4 | 4 | import ca.uhn.fhir.jpa.config.HibernatePropertiesProvider;
|
| 5 | +import ca.uhn.fhir.util.ReflectionUtil; |
5 | 6 | import org.hibernate.dialect.Dialect;
|
6 | 7 | import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver;
|
7 | 8 | import org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter;
|
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
8 | 11 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
9 | 12 |
|
10 | 13 | import java.sql.Connection;
|
11 | 14 | import java.sql.SQLException;
|
12 | 15 | import javax.sql.DataSource;
|
13 | 16 |
|
| 17 | +import static org.apache.commons.lang3.StringUtils.isNotBlank; |
| 18 | + |
14 | 19 | public class JpaHibernatePropertiesProvider extends HibernatePropertiesProvider {
|
| 20 | + private static final Logger ourLog = LoggerFactory.getLogger(JpaHibernatePropertiesProvider.class); |
15 | 21 |
|
16 |
| - private final Dialect dialect; |
| 22 | + private final Dialect myDialect; |
17 | 23 |
|
18 |
| - public JpaHibernatePropertiesProvider(LocalContainerEntityManagerFactoryBean myEntityManagerFactory) { |
19 |
| - DataSource connection = myEntityManagerFactory.getDataSource(); |
20 |
| - try (Connection dbConnection = connection.getConnection()) { |
21 |
| - dialect = new StandardDialectResolver() |
| 24 | + public JpaHibernatePropertiesProvider(LocalContainerEntityManagerFactoryBean theEntityManagerFactory) { |
| 25 | + String dialectClass = |
| 26 | + (String) theEntityManagerFactory.getJpaPropertyMap().get("hibernate.dialect"); |
| 27 | + if (isNotBlank(dialectClass)) { |
| 28 | + myDialect = ReflectionUtil.newInstanceOrReturnNull(dialectClass, Dialect.class); |
| 29 | + } else { |
| 30 | + ourLog.warn("'hibernate.dialect' not set in application configuration! Please explicitly specify a valid HAPI FHIR hibernate dialect."); |
| 31 | + DataSource connection = theEntityManagerFactory.getDataSource(); |
| 32 | + try (Connection dbConnection = connection.getConnection()) { |
| 33 | + myDialect = new StandardDialectResolver() |
22 | 34 | .resolveDialect(new DatabaseMetaDataDialectResolutionInfoAdapter(dbConnection.getMetaData()));
|
23 |
| - } catch (SQLException sqlException) { |
24 |
| - throw new ConfigurationException(sqlException.getMessage(), sqlException); |
| 35 | + } catch (SQLException sqlException) { |
| 36 | + throw new ConfigurationException(sqlException.getMessage(), sqlException); |
| 37 | + } |
25 | 38 | }
|
26 | 39 | }
|
27 | 40 |
|
28 | 41 | @Override
|
29 | 42 | public Dialect getDialect() {
|
30 |
| - return dialect; |
| 43 | + return myDialect; |
31 | 44 | }
|
32 | 45 | }
|
0 commit comments