Example usage for org.springframework.orm.jpa LocalContainerEntityManagerFactoryBean setPackagesToScan

List of usage examples for org.springframework.orm.jpa LocalContainerEntityManagerFactoryBean setPackagesToScan

Introduction

In this page you can find the example usage for org.springframework.orm.jpa LocalContainerEntityManagerFactoryBean setPackagesToScan.

Prototype

public void setPackagesToScan(String... packagesToScan) 

Source Link

Document

Set whether to use Spring-based scanning for entity classes in the classpath instead of using JPA's standard scanning of jar files with persistence.xml markers in them.

Usage

From source file:fredboat.db.DatabaseManager.java

/**
 * @param jdbcUrl connection to the database
 * @param dialect set to null or empty String to have it autodetected by Hibernate, chosen jdbc driver must support that
 *//*from   w w w  .java2  s .c  o  m*/
public static void startup(String jdbcUrl, String dialect, int poolSize) {
    state = DatabaseState.INITIALIZING;

    try {

        if (Config.CONFIG.isUseSshTunnel()) {
            connectSSH();
        }

        //These are now located in the resources directory as XML
        Properties properties = new Properties();
        properties.put("configLocation", "hibernate.cfg.xml");

        properties.put("hibernate.connection.provider_class",
                "org.hibernate.hikaricp.internal.HikariCPConnectionProvider");
        properties.put("hibernate.connection.url", jdbcUrl);
        if (dialect != null && !"".equals(dialect))
            properties.put("hibernate.dialect", dialect);
        properties.put("hibernate.cache.region.factory_class",
                "org.hibernate.cache.ehcache.EhCacheRegionFactory");

        //properties.put("hibernate.show_sql", "true");

        //automatically update the tables we need
        //caution: only add new columns, don't remove or alter old ones, otherwise manual db table migration needed
        properties.put("hibernate.hbm2ddl.auto", "update");

        properties.put("hibernate.hikari.maximumPoolSize", Integer.toString(poolSize));
        properties.put("hibernate.hikari.idleTimeout", Integer.toString(Config.HIKARI_TIMEOUT_MILLISECONDS));

        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        emfb.setPackagesToScan("fredboat.db.entity");
        emfb.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        emfb.setJpaProperties(properties);
        emfb.setPersistenceUnitName("fredboat.test");
        emfb.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        emfb.afterPropertiesSet();
        emf = emfb.getObject();

        log.info("Started Hibernate");
        state = DatabaseState.READY;
    } catch (Exception ex) {
        state = DatabaseState.FAILED;
        throw new RuntimeException("Failed starting database connection", ex);
    }
}

From source file:br.com.joaops.smt.configuration.PersistenceConfig.java

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setPackagesToScan(this.getPackagesToScan());
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(getAdditionalProperties());
    return em;//from  w  ww . j a  v  a 2s  . c om
}

From source file:com.ge.predix.acs.config.AcsConfigUtil.java

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);//from w w w .  java  2  s.c om
    em.setPackagesToScan(new String[] { "com.ge.predix.acs.service.policy.admin.dao",
            "com.ge.predix.acs.privilege.management.dao", "com.ge.predix.acs.zone.management.dao",
            "com.ge.predix.acs.attribute.connector.management.dao" });

    JpaVendorAdapter vendorAdapter = new OpenJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    return em;
}

From source file:uk.co.parso.barebones.DbConfig.java

@Bean(name = "testEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean testEntityManagerFactory() throws SQLException {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setPackagesToScan("uk.co.parso.barebones.entities");
    factory.setDataSource(testDataSource());
    factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
    factory.setJpaProperties(hibProperties());
    factory.afterPropertiesSet();/*from  ww w .  j  a  va  2 s .  c o m*/
    factory.setJpaDialect(new HibernateJpaDialect());

    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setDatabase(Database.MYSQL);
    factory.setJpaVendorAdapter(adapter);

    return factory;
}

From source file:org.ameba.samples.tenancy.TenancySampleApplication.java

public @Bean EntityManagerFactory customEntityManagerFactory(DataSource dataSource) {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(false); // turn off with Discriminator strategy so far!
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan(TenancySampleApplication.class.getPackage().getName());
    factory.setDataSource(dataSource);/*from   w ww.  ja  va  2  s.c  om*/
    factory.getJpaPropertyMap().put(Environment.DIALECT, PostgreSQL9Dialect.class.getName());
    factory.getJpaPropertyMap().put(Environment.MULTI_TENANT, MultiTenancyStrategy.DISCRIMINATOR);
    factory.getJpaPropertyMap().put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, new TenantHolder());
    factory.afterPropertiesSet();
    return factory.getObject();
}

From source file:com.iopr.PersistenceJPAConfig.java

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());/*from  w w w.  ja v  a 2s .c  om*/
    em.setPackagesToScan(new String[] { "com.iopr.model" });
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());
    return em;
}

From source file:com.volho.example.todo.web.config.PersistenceConfiguration.java

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);/*from ww  w. j a  v a2s .  c  o  m*/
    em.setPackagesToScan(new String[] { "com.volho.example.todo.web.model" });

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setShowSql(true);

    em.setJpaVendorAdapter(vendorAdapter);
    return em;
}

From source file:io.gravitee.repository.jdbc.config.JdbcRepositoryConfiguration.java

@Bean
public LocalContainerEntityManagerFactoryBean graviteeEntityManagerFactory(DataSource dataSource) {
    final Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.show_sql", showSql);

    final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setPackagesToScan("io.gravitee.repository.jdbc.model");
    entityManagerFactoryBean.setJpaProperties(hibernateProperties);
    entityManagerFactoryBean.setPersistenceProvider(new HibernatePersistenceProvider());
    entityManagerFactoryBean.setPersistenceUnitName("graviteePU");
    entityManagerFactoryBean.setDataSource(dataSource);
    return entityManagerFactoryBean;
}

From source file:com.github.fharms.camel.entitymanager.config.PersistenceJPAConfig.java

@Bean(name = "emf")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setPackagesToScan("com.github.fharms");

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;/*from  w  ww  .j a v a  2  s .  com*/
}

From source file:com.github.fharms.camel.entitymanager.config.PersistenceJPAConfig.java

@Bean(name = "emf2")
public LocalContainerEntityManagerFactoryBean entityManagerFactory2() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setPackagesToScan("com.github.fharms");

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;/*from w  ww  .j a  va  2s .c o m*/
}