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

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

Introduction

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

Prototype

public void setDataSource(DataSource dataSource) 

Source Link

Document

Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database.

Usage

From source file:com.qpark.eip.core.model.analysis.config.EipModelAnalysisPersistenceConfig.java

/**
 * Get the {@link LocalContainerEntityManagerFactoryBean}.
 *
 * @return the {@link LocalContainerEntityManagerFactoryBean}.
 */// w  w  w  .j  a  v a 2 s  .com
@Bean(name = ENTITY_MANAGER_FACTORY_NAME)
public EntityManagerFactory getEntityManagerFactory() {
    AbstractJpaVendorAdapter jpaVendorAdapter = this.getJpaVendorAdapter();
    if (jpaVendorAdapter == null) {
        throw new RuntimeException(String.format("%s jpaVendorAdpater not set properly %s.",
                ENTITY_MANAGER_FACTORY_NAME, String.valueOf(jpaVendorAdapter)));
    }
    String jpaVendorAdapterDatabasePlatform = this.jpaVendorAdapterConfiguration
            .getJpaVendorAdpaterDatabasePlatform();
    if (jpaVendorAdapterDatabasePlatform == null || jpaVendorAdapterDatabasePlatform.trim().length() == 0) {
        throw new RuntimeException(String.format("%s jpaVendorAdpaterDatabasePlatform not set properly %s.",
                ENTITY_MANAGER_FACTORY_NAME, String.valueOf(jpaVendorAdapterDatabasePlatform)));
    }

    LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
    bean.setPersistenceXmlLocation(
            String.format("classpath:/META-INF/%s/persistence.xml", PERSISTENCE_UNIT_NAME));
    bean.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
    bean.setDataSource(this.dataSource);

    jpaVendorAdapter.setDatabasePlatform(jpaVendorAdapterDatabasePlatform);
    jpaVendorAdapter.setShowSql(false);
    if (this.isJpaVendorAdapterGenerateDdl()) {
        jpaVendorAdapter.setGenerateDdl(true);
        if (HibernateJpaVendorAdapter.class.isInstance(jpaVendorAdapter)) {
            bean.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", "update");
        }
    } else {
        jpaVendorAdapter.setGenerateDdl(false);
    }

    bean.setJpaVendorAdapter(jpaVendorAdapter);
    bean.afterPropertiesSet();
    return bean.getObject();
}

From source file:cz.lbenda.coursing.client.ClientAppConfig.java

public @Bean EntityManagerFactory entityManagerFactory() {
    try {/*from   www.j av  a  2s .  c o  m*/
        EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.H2Platform");
        vendorAdapter.setShowSql(true);
        vendorAdapter.setGenerateDdl(true);
        EclipseLinkJpaDialect dialect = new EclipseLinkJpaDialect();
        dialect.setLazyDatabaseTransaction(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setJpaDialect(dialect);
        factory.setPackagesToScan("cz.lbenda.coursing");
        factory.setDataSource(dataSource());
        factory.setPersistenceXmlLocation("classpath*:META-INF/persistence.xml");
        factory.setPersistenceUnitName("coursing");

        /*
        Map<String, String> prop = new HashMap<>();
        prop.put("eclipselink.deploy-on-startup", "true");
        prop.put("eclipselink.ddl-generation", "create-or-extend-tables");
        prop.put("eclipselink.ddl-generation.output-mode", "database");
        prop.put("eclipselink.create-ddl-jdbc-file-name", "create.sql");
        prop.put("eclipselink.weaving", "static");
        prop.put("eclipselink.weaving.lazy", "true");
        prop.put("eclipselink.weaving.internal", "true");
        prop.put("eclipselink.logging.level", "SEVERE");
        prop.put("eclipselink.query-results-cache.type", "WEAK");
        prop.put("eclipselink.jdbc.batch-writing", "JDBC");
        prop.put("eclipselink.jdbc.batch-writing.size", "1000");
                
        factory.setJpaPropertyMap(prop);
        */

        // factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        factory.afterPropertiesSet();

        return factory.getObject();
    } catch (Exception e) {
        LOG.trace("Faild create entityManagerFactory", e);
        throw new RuntimeException("Faild create entityManagerFactory", e);
    }
}

From source file:hub.config.jpa.HubConfigJpa.java

@Bean
public EntityManagerFactory entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);//from  ww w. j  ava2s. c o  m
    vendorAdapter.setShowSql(false);
    vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("things.thing", "hub.backends.users.types");
    factory.setDataSource(dataSource());
    factory.setMappingResources("thing.hbm.xml");
    factory.afterPropertiesSet();

    return factory.getObject();
}

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:org.lightmare.jpa.spring.SpringORM.java

/**
 * Creates {@link LocalContainerEntityManagerFactoryBean} for container
 * scoped use/*from w  w w  . j  av  a2  s .co m*/
 * 
 * @return {@link LocalContainerEntityManagerFactoryBean}
 */
private LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactoryBean.setPersistenceUnitName(unitName);
    // Checks data source type
    if (swapDataSources) {
        entityManagerFactoryBean.setDataSource(dataSource);
    } else {
        addJtaDatasource(entityManagerFactoryBean);
    }

    if (ObjectUtils.notNull(loader)) {
        entityManagerFactoryBean.setBeanClassLoader(loader);
    }

    // entityManagerFactoryBean.setPackagesToScan();
    entityManagerFactoryBean.setPersistenceProvider(persistenceProvider);
    if (CollectionUtils.valid(properties)) {
        entityManagerFactoryBean.setJpaProperties(properties);
    }

    // Configures JPA ORM system for use
    entityManagerFactoryBean.afterPropertiesSet();

    return entityManagerFactoryBean;
}

From source file:com.qpark.eip.core.spring.lockedoperation.config.EipLockedoperationConfig.java

/**
 * Get the {@link LocalContainerEntityManagerFactoryBean}.
 *
 * @return the {@link LocalContainerEntityManagerFactoryBean}.
 *///from  ww  w. j  av a 2 s.c om
@Bean(name = ENTITY_MANAGER_FACTORY_NAME)
public EntityManagerFactory getEntityManagerFactory() {
    AbstractJpaVendorAdapter jpaVendorAdapter = this.getJpaVendorAdapter();
    if (jpaVendorAdapter == null) {
        throw new RuntimeException(String.format("%s jpaVendorAdpater not set properly %s.",
                ENTITY_MANAGER_FACTORY_NAME, String.valueOf(jpaVendorAdapter)));
    }

    String jpaVendorAdapterDatabasePlatform = this.jpaVendorAdapterConfiguration
            .getJpaVendorAdpaterDatabasePlatform();
    if (jpaVendorAdapterDatabasePlatform == null || jpaVendorAdapterDatabasePlatform.trim().length() == 0) {
        throw new RuntimeException(String.format("%s jpaVendorAdpaterDatabasePlatform not set properly %s.",
                ENTITY_MANAGER_FACTORY_NAME, String.valueOf(jpaVendorAdapterDatabasePlatform)));
    }

    LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
    bean.setPersistenceXmlLocation(new StringBuffer(96).append("classpath:/META-INF/")
            .append(PERSISTENCE_UNIT_NAME).append("/persistence.xml").toString());
    bean.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
    bean.setDataSource(this.datasource);

    jpaVendorAdapter.setDatabasePlatform(jpaVendorAdapterDatabasePlatform);
    jpaVendorAdapter.setShowSql(false);
    if (this.isJpaVendorAdapterGenerateDdl()) {
        jpaVendorAdapter.setGenerateDdl(true);
        if (HibernateJpaVendorAdapter.class.isInstance(jpaVendorAdapter)) {
            bean.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", "update");
        }
    } else {
        jpaVendorAdapter.setGenerateDdl(false);
    }

    bean.setJpaVendorAdapter(jpaVendorAdapter);
    bean.afterPropertiesSet();
    return bean.getObject();
}

From source file:br.com.alura.casadocodigo.conf.JPAConfiguration.java

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

    JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    factoryBean.setJpaVendorAdapter(adapter);

    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setUsername("root");
    dataSource.setPassword("");
    dataSource.setUrl("jdbc:mysql://localhost:3306/casadocodigo");
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    factoryBean.setDataSource(dataSource);

    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    properties.setProperty("hibernate.show_sql", "true");
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    factoryBean.setJpaProperties(properties);

    factoryBean.setPackagesToScan("br.com.alura.casadocodigo.models");

    return factoryBean;
}

From source file:com.alliander.osgp.webdevicesimulator.application.config.ApplicationContext.java

/**
 * Method for creating the Entity Manager Factory Bean.
 *
 * @return LocalContainerEntityManagerFactoryBean
 * @throws ClassNotFoundException//  ww w  .j  av a2s.  c  om
 *             when class not found
 */
@Bean
@DependsOn("flyway")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
    final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactoryBean.setPersistenceUnitName("OSPG_DEVICESIMULATOR_WEB");
    entityManagerFactoryBean.setDataSource(this.getDataSource());
    entityManagerFactoryBean.setPackagesToScan(
            this.environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
    entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);

    final Properties jpaProperties = new Properties();
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
            this.environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL,
            this.environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY,
            this.environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
            this.environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));

    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
}

From source file:org.drugis.addis.config.MainConfig.java

@Bean(name = "emAddisCore")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(false);
    vendorAdapter.setShowSql(false);/*from  w  ww  .  j a va2  s .co m*/
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setJpaProperties(additionalProperties());
    em.setJpaVendorAdapter(vendorAdapter);
    em.setPackagesToScan("org.drugis.addis.projects", "org.drugis.addis.outcomes",
            "org.drugis.addis.interventions", "org.drugis.addis.security", "org.drugis.addis.analyses",
            "org.drugis.addis.scenarios", "org.drugis.addis.models", "org.drugis.addis.problems",
            "org.drugis.addis.covariates", "org.drugis.trialverse", "org.drugis.addis.scaledUnits",
            "org.drugis.addis.subProblems", "org.drugis.addis.ordering", "org.drugis.addis.workspaceSettings");
    em.setDataSource(dataSource());
    em.setPersistenceUnitName("addisCore");
    em.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());

    em.afterPropertiesSet();
    return em;
}