Search in sources :

Example 1 with Database

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;
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) Database(org.springframework.orm.jpa.vendor.Database) DatabaseDriver(org.springframework.boot.jdbc.DatabaseDriver)

Example 2 with Database

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);
}
Also used : Connection(java.sql.Connection) Database(org.springframework.orm.jpa.vendor.Database) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource)

Example 3 with Database

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);
}
Also used : Database(org.springframework.orm.jpa.vendor.Database) Test(org.junit.Test)

Example 4 with Database

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;
}
Also used : HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) Database(org.springframework.orm.jpa.vendor.Database)

Example 5 with Database

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();
}
Also used : Database(org.springframework.orm.jpa.vendor.Database) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Aggregations

Database (org.springframework.orm.jpa.vendor.Database)10 Test (org.junit.Test)5 HibernateJpaVendorAdapter (org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter)4 DataSource (javax.sql.DataSource)2 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 Test (org.junit.jupiter.api.Test)1 DatabaseDriver (org.springframework.boot.jdbc.DatabaseDriver)1 Bean (org.springframework.context.annotation.Bean)1 MetaDataAccessException (org.springframework.jdbc.support.MetaDataAccessException)1 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)1