Search in sources :

Example 66 with LocalContainerEntityManagerFactoryBean

use of org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in project evosuite by EvoSuite.

the class EvoEntityManagerFactory method createEMFWithSpring.

private EntityManagerFactory createEMFWithSpring() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    // DriverManagerDataSource uses the context classloader for some reason...
    ClassLoader cl1 = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(EvoEntityManagerFactory.class.getClassLoader());
    dataSource.setDriverClassName(org.hsqldb.jdbcDriver.class.getName());
    dataSource.setUrl("jdbc:hsqldb:mem:.");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    Thread.currentThread().setContextClassLoader(cl1);
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    // search everything on classpath
    em.setPackagesToScan("");
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    try {
        /*
                The code in this class works fine on Mac, but somehow it crashes on Debian (Lux cluster).
                So, the following is just a workaround, although not fully understood while on Debian
                it was behaving differently
             */
        Field f = LocalContainerEntityManagerFactoryBean.class.getDeclaredField("internalPersistenceUnitManager");
        f.setAccessible(true);
        DefaultPersistenceUnitManager m = (DefaultPersistenceUnitManager) f.get(em);
        m.setDefaultPersistenceUnitRootLocation(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Properties properties = new Properties();
    properties.setProperty("hibernate.show_sql", "true");
    properties.setProperty("hibernate.dialect", HSQLDialect.class.getName());
    properties.setProperty("hibernate.connection.shutdown", "true");
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.classloading.use_current_tccl_as_parent", "false");
    em.setJpaProperties(properties);
    em.afterPropertiesSet();
    return em.getObject();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) Field(java.lang.reflect.Field) DefaultPersistenceUnitManager(org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) Properties(java.util.Properties) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 67 with LocalContainerEntityManagerFactoryBean

use of org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in project tutorials-java by Artister.

the class JPAConfig method entityManagerFactory.

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("org.ko.web");
    factory.setDataSource(dataSource);
    return factory;
}
Also used : 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)

Example 68 with LocalContainerEntityManagerFactoryBean

use of org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in project irida by phac-nml.

the class IridaApiRepositoriesConfig method entityManagerFactory.

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(dataSource);
    factory.setJpaVendorAdapter(jpaVendorAdapter);
    factory.setJpaProperties(dataConfig.getJpaProperties());
    factory.setPackagesToScan("ca.corefacility.bioinformatics.irida.model", "ca.corefacility.bioinformatics.irida.repositories.relational.auditing");
    return factory;
}
Also used : LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) EnversRevisionRepositoryFactoryBean(org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 69 with LocalContainerEntityManagerFactoryBean

use of org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in project Backend by FredBoat.

the class DatabaseConfiguration method databaseManager.

@Bean
public DatabaseManager databaseManager(HibernateStatisticsCollector hibernateStats, PrometheusMetricsTrackerFactory hikariStats) {
    DatabaseManager databaseManager = new DatabaseManager(hibernateStats, hikariStats, this.dbConf.getHikariPoolSize(), "Quarterdeck", true, this.dbConf.getMainJdbcUrl(), this.dbConf.getCacheJdbcUrl(), (puName, dataSource, properties, entityPackages) -> {
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        emfb.setDataSource(dataSource);
        emfb.setPackagesToScan(entityPackages.toArray(new String[entityPackages.size()]));
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        emfb.setJpaVendorAdapter(vendorAdapter);
        emfb.setJpaProperties(properties);
        // initiate creation of the native emf
        emfb.afterPropertiesSet();
        return emfb.getNativeEntityManagerFactory();
    });
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        if (databaseManager.isCacheConnBuilt()) {
            DatabaseConnection cacheDbConn = databaseManager.getCacheDbConn();
            if (cacheDbConn != null) {
                cacheDbConn.shutdown();
            }
        }
        if (databaseManager.isMainConnBuilt()) {
            databaseManager.getMainDbConn().shutdown();
        }
    }, "databasemanager-shutdown-hook"));
    return databaseManager;
}
Also used : DatabaseManager(com.fredboat.backend.quarterdeck.db.DatabaseManager) JpaVendorAdapter(org.springframework.orm.jpa.JpaVendorAdapter) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) DatabaseConnection(space.npstr.sqlsauce.DatabaseConnection) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 70 with LocalContainerEntityManagerFactoryBean

use of org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in project cloudbreak by hortonworks.

the class DatabaseConfig method entityManagerFactory.

@Bean
@DependsOn("databaseUpMigration")
public EntityManagerFactory entityManagerFactory() throws SQLException {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setPackagesToScan("com.sequenceiq.periscope.domain");
    entityManagerFactory.setDataSource(dataSource());
    entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
    entityManagerFactory.setJpaProperties(jpaProperties());
    entityManagerFactory.afterPropertiesSet();
    return entityManagerFactory.getObject();
}
Also used : LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) DependsOn(org.springframework.context.annotation.DependsOn) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Aggregations

LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)75 Bean (org.springframework.context.annotation.Bean)63 HibernateJpaVendorAdapter (org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter)42 Properties (java.util.Properties)14 JpaVendorAdapter (org.springframework.orm.jpa.JpaVendorAdapter)7 HashMap (java.util.HashMap)4 JpaConfigDataHolder (org.apereo.cas.configuration.model.support.jpa.JpaConfigDataHolder)4 Test (org.junit.jupiter.api.Test)4 DependsOn (org.springframework.context.annotation.DependsOn)4 Lazy (org.springframework.context.annotation.Lazy)4 AbstractJpaProperties (org.apereo.cas.configuration.model.support.jpa.AbstractJpaProperties)2 DatabaseProperties (org.apereo.cas.configuration.model.support.jpa.DatabaseProperties)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Primary (org.springframework.context.annotation.Primary)2 DefaultPersistenceUnitManager (org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager)2 DatabaseManager (com.fredboat.backend.quarterdeck.db.DatabaseManager)1 AugmentableQueryRepositoryFactoryBean (com.thinkbiganalytics.metadata.jpa.feed.AugmentableQueryRepositoryFactoryBean)1 DataSourceBean (de.alpharogroup.springconfig.DataSourceBean)1 JdbcUrlBean (de.alpharogroup.springconfig.JdbcUrlBean)1 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)1