use of org.springframework.orm.jpa.vendor.Database in project spring-boot by spring-projects.
the class DatabaseLookup method getDatabase.
/**
* Return the most suitable {@link Database} for the given {@link DataSource}.
* @param dataSource the source {@link DataSource}
* @return the most suitable {@link Database}
*/
public static Database getDatabase(DataSource dataSource) {
if (dataSource == null) {
return Database.DEFAULT;
}
try {
String url = (String) JdbcUtils.extractDatabaseMetaData(dataSource, "getURL");
DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
Database database = LOOKUP.get(driver);
if (database != null) {
return database;
}
} catch (MetaDataAccessException ex) {
logger.warn("Unable to determine jdbc url from datasource", ex);
}
return Database.DEFAULT;
}
use of org.springframework.orm.jpa.vendor.Database in project spring-boot by spring-projects.
the class DatabaseLookupTests method testGetDatabase.
private void testGetDatabase(String url, Database expected) throws Exception {
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
DatabaseMetaData metaData = mock(DatabaseMetaData.class);
given(dataSource.getConnection()).willReturn(connection);
given(connection.getMetaData()).willReturn(metaData);
given(metaData.getURL()).willReturn(url);
Database database = DatabaseLookup.getDatabase(dataSource);
assertThat(database).isEqualTo(expected);
}
use of org.springframework.orm.jpa.vendor.Database in project spring-boot by spring-projects.
the class JpaPropertiesTests method determineDatabaseWithKnownUrlAndUserConfig.
@Test
public void determineDatabaseWithKnownUrlAndUserConfig() {
JpaProperties properties = load("spring.jpa.database=mysql");
Database database = properties.determineDatabase(mockDataSource("jdbc:h2:mem:testdb"));
assertThat(database).isEqualTo(Database.MYSQL);
}
use of org.springframework.orm.jpa.vendor.Database in project herd by FINRAOS.
the class DaoSpringModuleConfig method getHibernateJpaVendorAdapter.
/**
* Gets the Hibernate JPA vendor adapter needed by the entity manager.
*
* @return the Hibernate JPA vendor adapter.
*/
private JpaVendorAdapter getHibernateJpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
// Set the database type.
String databaseType = configurationHelper.getProperty(ConfigurationValue.DATABASE_TYPE);
if (StringUtils.isBlank(databaseType)) {
throw new IllegalStateException(String.format("No database type found. Ensure the \"%s\" configuration entry is configured.", ConfigurationValue.DATABASE_TYPE.getKey()));
}
Database database = Database.valueOf(databaseType);
LOGGER.info("jpaTargetDatabase={}", database);
hibernateJpaVendorAdapter.setDatabase(database);
hibernateJpaVendorAdapter.setGenerateDdl(false);
return hibernateJpaVendorAdapter;
}
use of org.springframework.orm.jpa.vendor.Database in project gpconnect-demonstrator by nhsconnect.
the class DataSourceConfig method entityManagerFactory.
@Bean
public EntityManagerFactory entityManagerFactory(DataSource dataSource) {
final Database database = Database.valueOf(vendor.toUpperCase());
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(showSql);
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setDatabase(database);
final LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("uk.gov.hscic");
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
return factory.getObject();
}
Aggregations