Java Code Examples for org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean#setJpaPropertyMap()

The following examples show how to use org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean#setJpaPropertyMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomerConfig.java    From spring-boot-jta-atomikos-sample with Apache License 2.0 6 votes vote down vote up
@Primary
@Bean(name = "customerEntityManager")
@DependsOn("transactionManager")
public LocalContainerEntityManagerFactoryBean customerEntityManager() throws Throwable {

	HashMap<String, Object> properties = new HashMap<String, Object>();
	properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
	properties.put("javax.persistence.transactionType", "JTA");

	LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
	entityManager.setJtaDataSource(customerDataSource());
	entityManager.setJpaVendorAdapter(jpaVendorAdapter);
	entityManager.setPackagesToScan("com.iyihua.sample.domain.customer");
	entityManager.setPersistenceUnitName("customerPersistenceUnit");
	entityManager.setJpaPropertyMap(properties);
	return entityManager;
}
 
Example 2
Source File: PersistenceUserConfiguration.java    From tutorials with MIT License 6 votes vote down vote up
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean userEntityManager() {
    System.out.println("loading config");
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(userDataSource());
    em.setPackagesToScan("com.baeldung.multipledb.model.user");

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
    em.setJpaPropertyMap(properties);

    return em;
}
 
Example 3
Source File: PersistenceUserAutoConfiguration.java    From tutorials with MIT License 6 votes vote down vote up
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean userEntityManager() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(userDataSource());
    em.setPackagesToScan("com.baeldung.multipledb.model.user");

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
    em.setJpaPropertyMap(properties);

    return em;
}
 
Example 4
Source File: ManyToManyTestConfiguration.java    From tutorials with MIT License 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${hibernate.hbm2ddl.auto}") String hbm2ddlType, @Value("${hibernate.dialect}") String dialect, @Value("${hibernate.show_sql}") boolean showSql) {
    LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean();

    result.setDataSource(dataSource());
    result.setPackagesToScan("com.baeldung.manytomany.model");
    result.setJpaVendorAdapter(jpaVendorAdapter());

    Map<String, Object> jpaProperties = new HashMap<>();
    jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddlType);
    jpaProperties.put("hibernate.dialect", dialect);
    jpaProperties.put("hibernate.show_sql", showSql);
    result.setJpaPropertyMap(jpaProperties);

    return result;
}
 
Example 5
Source File: HibernateConfig.java    From spring-boot-multitenant with Apache License 2.0 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
    MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
    CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl) {
  Map<String, Object> properties = new HashMap<>();
  properties.putAll(jpaProperties.getHibernateProperties(dataSource));
  properties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
  properties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl);
  properties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl);

  LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  em.setDataSource(dataSource);
  em.setPackagesToScan("com.srai");
  em.setJpaVendorAdapter(jpaVendorAdapter());
  em.setJpaPropertyMap(properties);
  return em;
}
 
Example 6
Source File: HibernateConfig.java    From cloud-s4-sdk-examples with Apache License 2.0 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProvider,
                                                                   CurrentTenantIdentifierResolver tenantIdentifierResolver) {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setPackagesToScan("com.mycompany.models");

    em.setJpaVendorAdapter(this.jpaVendorAdapter());

    final Map<String, Object> jpaProperties = new HashMap<>();
    jpaProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
    jpaProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
    jpaProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifierResolver);
    jpaProperties.put(Environment.FORMAT_SQL, true);

    em.setJpaPropertyMap(jpaProperties);
    return em;
}
 
Example 7
Source File: DataConfig.java    From pizzeria with MIT License 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setShowSql(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(dataSource());
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("pzinsta.pizzeria.model");
    factory.setJpaPropertyMap(
            ImmutableMap.of(
                    "hibernate.dialect", environment.getProperty("hibernate.dialect"),
                    "hibernate.hbm2ddl.auto", environment.getProperty("hibernate.hbm2ddl.auto"),
                    "hibernate.hbm2ddl.import_files_sql_extractor", environment.getProperty("hibernate.hbm2ddl.import_files_sql_extractor"),
                    "hibernate.hbm2ddl.import_files", environment.getProperty("hibernate.hbm2ddl.import_files")
            ));
    return factory;
}
 
Example 8
Source File: MultiTenancyJpaConfiguration.java    From multitenancy with Apache License 2.0 6 votes vote down vote up
/**
 * org.springframework.beans.factory.FactoryBean that creates a JPA
 * {@link javax.persistence.EntityManagerFactory} according to JPA's standard
 * container bootstrap contract. This is the most powerful way to set up a
 * shared JPA EntityManagerFactory in a Spring application context; the
 * EntityManagerFactory can then be passed to JPA-based DAOs via dependency
 * injection. Note that switching to a JNDI lookup or to a
 * {@link org.springframework.orm.jpa.LocalEntityManagerFactoryBean} definition
 * is just a matter of configuration!
 * 
 * @param multiTenantConnectionProvider
 * @param currentTenantIdentifierResolver
 * @return
 */
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
        MultiTenantConnectionProvider multiTenantConnectionProvider,
        CurrentTenantIdentifierResolver currentTenantIdentifierResolver) {

    Map<String, Object> hibernateProps = new LinkedHashMap<>();
    hibernateProps.putAll(this.jpaProperties.getProperties());
    hibernateProps.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
    hibernateProps.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
    hibernateProps.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);

    // No dataSource is set to resulting entityManagerFactoryBean
    LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean();
    result.setPackagesToScan(new String[] { Employee.class.getPackage().getName() });
    result.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    result.setJpaPropertyMap(hibernateProps);

    return result;
}
 
Example 9
Source File: Utils.java    From radman with MIT License 6 votes vote down vote up
static LocalContainerEntityManagerFactoryBean buildEntityManager(DataSource dataSource,
                                                                 JpaProperties jpaProperties,
                                                                 String entitiesPackage,
                                                                 String persistenceUnitName) {
    LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
    entityManager.setDataSource(dataSource);
    entityManager.setPackagesToScan(entitiesPackage);
    entityManager.setPersistenceUnitName(persistenceUnitName);
    entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.hbm2ddl.auto", jpaProperties.getHibernate().getDdlAuto());
    properties.put("hibernate.dialect", jpaProperties.getHibernate().getDialect());
    properties.put("show-sql", jpaProperties.getShowSql());

    entityManager.setJpaPropertyMap(properties);
    return entityManager;
}
 
Example 10
Source File: OrderConfig.java    From spring-boot-jta-atomikos-sample with Apache License 2.0 5 votes vote down vote up
@Bean(name = "orderEntityManager")
public LocalContainerEntityManagerFactoryBean orderEntityManager() throws Throwable {

	HashMap<String, Object> properties = new HashMap<String, Object>();
	properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
	properties.put("javax.persistence.transactionType", "JTA");

	LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
	entityManager.setJtaDataSource(orderDataSource());
	entityManager.setJpaVendorAdapter(jpaVendorAdapter);
	entityManager.setPackagesToScan("com.iyihua.sample.domain.order");
	entityManager.setPersistenceUnitName("orderPersistenceUnit");
	entityManager.setJpaPropertyMap(properties);
	return entityManager;
}
 
Example 11
Source File: DatabaseConfig.java    From we-cmdb with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
    emfb.setDataSource(dataSource);
    emfb.setJpaVendorAdapter(jpaVendorAdapter);
    emfb.setPackagesToScan("com.webank.cmdb.domain");
    emfb.setJpaPropertyMap(getCustomizedProperties());
    return emfb;
}
 
Example 12
Source File: JpaConfiguration.java    From ps-guitar-db with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
	lef.setDataSource(this.dataSource);
	lef.setJpaPropertyMap(this.jpaProperties());
	lef.setJpaVendorAdapter(this.jpaVendorAdapter());
	return lef;
}
 
Example 13
Source File: EmbeddedDataSourceConfig.java    From spring-sync with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
		lef.setDataSource(dataSource());
		lef.setJpaPropertyMap(this.jpaProperties());
		lef.setJpaVendorAdapter(this.jpaVendorAdapter());
		lef.setPackagesToScan("org.springframework.sync");
		return lef;
}
 
Example 14
Source File: JpaConfig.java    From tessera with Apache License 2.0 5 votes vote down vote up
@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(final DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean =
                new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setDataSource(dataSource);
        localContainerEntityManagerFactoryBean.setJpaDialect(new EclipseLinkJpaDialect());
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());

        localContainerEntityManagerFactoryBean.setJpaPropertyMap(
                new HashMap<String, String>() {
                    {
                        put("eclipselink.logging.logger", "org.eclipse.persistence.logging.slf4j.SLF4JLogger");
                        put("eclipselink.weaving", "false");
                        //  put("eclipselink.logging.level.sql","FINE");
                        // put("eclipselink.logging.parameters","true");
                        put("javax.persistence.schema-generation.database.action", "create");
//                        put("javax.persistence.schema-generation.scripts.action", "create");
//                        put(
//                                "javax.persistence.schema-generation.scripts.create-target",
//                                "target/" + getCreateScriptName());
                    }
                });

        /*
           <property name="jpaVendorAdapter">
               <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                   <property name="showSql" value="true"/>
                   <property name="generateDdl" value="true"/>
                   <property name="databasePlatform" value="org.eclipse.persistence.platform.database.H2Platform"/>
               </bean>
           </property>
        */
        return localContainerEntityManagerFactoryBean;
    }
 
Example 15
Source File: PersistenceProductConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(productDataSource());
    em.setPackagesToScan("com.baeldung.multipledb.model.product");

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
    em.setJpaPropertyMap(properties);

    return em;
}
 
Example 16
Source File: PersistenceProductConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(productDataSource());
    em.setPackagesToScan("com.baeldung.multipledb.model.product");

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
    em.setJpaPropertyMap(properties);

    return em;
}
 
Example 17
Source File: PersistenceProductAutoConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(productDataSource());
    em.setPackagesToScan("com.baeldung.multipledb.model.product");

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
    em.setJpaPropertyMap(properties);

    return em;
}
 
Example 18
Source File: JpaConfiguration.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
	lef.setDataSource(this.dataSource);
	lef.setJpaPropertyMap(this.jpaProperties());
	lef.setJpaVendorAdapter(this.jpaVendorAdapter());
	return lef;
}
 
Example 19
Source File: JPAStartupService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Initialize JPA objects (Datasource, Persistence Unit Manager, Entity Manager Factory, Transaction Manager) for each pool.
 */
public void process( )
{
    ReferenceList list = new ReferenceList( );
    AppConnectionService.getPoolList( list );

    Map<String, EntityManagerFactory> mapFactories = new HashMap<>( );
    List<PlatformTransactionManager> listTransactionManagers = new ArrayList<>( );
    _log.info( "JPA Startup Service : Initializing JPA objects ..." );

    String strDialectProperty = AppPropertiesService.getProperty( JPA_DIALECT_PROPERTY );

    for ( ReferenceItem poolItem : list )
    {
        String strPoolname = poolItem.getCode( );

        DataSource ds = AppConnectionService.getPoolManager( ).getDataSource( strPoolname );
        _log.info( "JPA Startup Service : DataSource retrieved for pool : " + strPoolname );
        _log.debug( "> DS : " + ds.toString( ) );

        DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager( );
        pum.setDefaultDataSource( ds );

        PersistenceUnitPostProcessor [ ] postProcessors = {
                new JPAPersistenceUnitPostProcessor( )
        };
        pum.setPersistenceUnitPostProcessors( postProcessors );

        pum.afterPropertiesSet( );

        _log.info( "JPA Startup Service : Persistence Unit Manager for pool : " + strPoolname );
        _log.debug( "> PUM : " + pum.toString( ) );

        LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean( );
        lcemfb.setDataSource( ds );
        lcemfb.setPersistenceUnitManager( pum );
        lcemfb.setPersistenceUnitName( "jpaLuteceUnit" );

        JpaDialect jpaDialect = SpringContextService.getBean( "jpaDialect" );
        lcemfb.setJpaDialect( jpaDialect );

        Map mapJpaProperties = SpringContextService.getBean( "jpaPropertiesMap" );
        lcemfb.setJpaPropertyMap( mapJpaProperties );

        String strDialect = AppPropertiesService.getProperty( poolItem.getName( ) + ".dialect" );

        // replace default dialect if <poolname>.dialect is specified
        if ( StringUtils.isNotBlank( strDialect ) )
        {
            mapJpaProperties.put( strDialectProperty, strDialect );
        }

        _log.debug( "Using dialect " + mapJpaProperties.get( strDialectProperty ) + " for pool " + poolItem.getName( ) );

        JpaVendorAdapter jpaVendorAdapter = SpringContextService.getBean( "jpaVendorAdapter" );
        lcemfb.setJpaVendorAdapter( jpaVendorAdapter );

        lcemfb.afterPropertiesSet( );

        EntityManagerFactory emf = lcemfb.getNativeEntityManagerFactory( );
        _log.info( "JPA Startup Service : EntityManagerFactory created for pool : " + strPoolname );
        _log.debug( "> EMF : " + emf.toString( ) );

        JpaTransactionManager tm = new JpaTransactionManager( );
        tm.setEntityManagerFactory( emf );
        tm.setJpaDialect( jpaDialect );
        _log.debug( "> JpaDialect " + jpaDialect );
        tm.afterPropertiesSet( );
        _log.info( "JPA Startup Service : JPA TransactionManager created for pool : " + strPoolname );
        _log.debug( "> TM : " + tm.toString( ) );

        mapFactories.put( strPoolname, emf );
        listTransactionManagers.add( tm );
    }

    EntityManagerService ems = SpringContextService.getBean( "entityManagerService" );
    ems.setMapFactories( mapFactories );

    ChainedTransactionManager ctm = SpringContextService.getBean( "transactionManager" );
    ctm.setTransactionManagers( listTransactionManagers );
    _log.info( "JPA Startup Service : completed successfully" );
}