org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean Java Examples

The following examples show how to use org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean. 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: RdbmsPersistenceConfig.java    From devicehive-java-server with Apache License 2.0 7 votes vote down vote up
@Bean
@DependsOn(value = {"simpleApplicationContextHolder"})
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
    factoryBean.setValidationMode(ValidationMode.CALLBACK);
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    factoryBean.setPackagesToScan("com.devicehive.model");

    final Properties props = new Properties();
    props.putAll(this.properties.getHibernateProperties(this.dataSource));
    if (useNativeClient) {
        props.put("hibernate.cache.hazelcast.native_client_group", groupName);
        props.put("hibernate.cache.hazelcast.native_client_password", groupPassword);
        if (!CollectionUtils.isEmpty(clusterMembers)) {
            props.put("hibernate.cache.hazelcast.native_client_address", clusterMembers.get(0));
        }
    }

    factoryBean.setJpaProperties(props);
    return factoryBean;
}
 
Example #2
Source File: SecondaryConfig.java    From txle with Apache License 2.0 7 votes vote down vote up
@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("secondaryDataSource") DataSource secondaryDataSource, EntityManagerFactoryBuilder builder) {
    return builder.dataSource(secondaryDataSource)
            .properties(jpaProperties.getProperties())
            .packages("com.actionsky.txle.grpc.entity")
            .persistenceUnit("secondaryPersistenceUnit")
            .build();
}
 
Example #3
Source File: EntityManagerFactoryServiceImpl.java    From multitenant with Apache License 2.0 6 votes vote down vote up
@Override
public EntityManagerFactory createTempEntityManagerFactory(
		Organization organization) {
	SingleConnectionDataSource dataSource = dataSourceService.createSingleConnectionDataSource(organization);
	if (dataSource != null) {
		Map<String, Object> vendorProperties = getVendorProperties(dataSource);
		customizeVendorProperties(vendorProperties);
	    Builder builder = getEntityManagerFactoryBuilder(dataSource).dataSource(dataSource).packages(mergePackagesToScan())
				.properties(vendorProperties).jta(isJta());
		LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = builder.build();
		entityManagerFactoryBean.setBeanClassLoader(classLoader);
		entityManagerFactoryBean.setBeanFactory(beanFactory);
		entityManagerFactoryBean.setBeanName(beanName);
		entityManagerFactoryBean.setLoadTimeWeaver(loadTimeWeaver);
		entityManagerFactoryBean.setResourceLoader(resourceLoader);
		entityManagerFactoryBean.afterPropertiesSet();
		return entityManagerFactoryBean.getObject();
	}
	return null;
}
 
Example #4
Source File: DataEnversConfig.java    From spring4-sandbox with Apache License 2.0 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
	emf.setDataSource(dataSource());
	emf.setPackagesToScan("com.hantsylabs.example.spring.model");
	emf.setPersistenceProvider(new HibernatePersistenceProvider());
	emf.setJpaProperties(jpaProperties());
	return emf;
}
 
Example #5
Source File: DaoConfig.java    From careconnect-reference-implementation with Apache License 2.0 6 votes vote down vote up
@Bean
public EntityManagerFactory entityManagerFactory() {
    //  final Database database = Database.valueOf(vendor.toUpperCase());

    final LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setPersistenceUnitName("CCRI_PERSISTENCE_UNIT");
    // factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("uk.nhs.careconnect.ri.database.entity");
    factory.setDataSource(dataSource());
    factory.setPersistenceProvider(new HibernatePersistenceProvider());
    factory.setJpaProperties(jpaProperties());
    factory.afterPropertiesSet();


    return factory.getObject();
}
 
Example #6
Source File: JpaCoreConfig.java    From bearchoke with Apache License 2.0 6 votes vote down vote up
@Bean
public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(dataSource);
    lef.setJpaVendorAdapter(jpaVendorAdapter());
    String[] packages = environment.getProperty("jpa.packages.to.scan").split(",");
    lef.setPackagesToScan(packages);

    Properties props = new Properties();
    props.put("hibernate.show_sql", "true");
    props.put("hibernate.format_sql", "true");
    props.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
    props.put("hibernate.connection.charSet", "UTF-8");
    props.put("hibernate.current_session_context_class", "jta");
    props.put("hibernate.archive.autodetection", "class");
    props.put("hibernate.transaction.manager_lookup_class", "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
    props.put("hibernate.dialect", environment.getProperty("jpa.dialect"));
    props.put("hibernate.hbm2ddl.auto", environment.getProperty("jpa.hibernate.create.strategy"));
    lef.setJpaProperties(props);

    lef.afterPropertiesSet();

    return lef.getObject();
}
 
Example #7
Source File: TaskDbConfig.java    From app-engine with Apache License 2.0 6 votes vote down vote up
@Bean
LocalContainerEntityManagerFactoryBean taskEntityManagerFactory() {

    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

    factoryBean.setDataSource(dataSource);
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    //此处应包含当前模块的domain类
    String packageName = TaskDbConfig.class.getPackage().getName();
    factoryBean.setPackagesToScan(StringUtils.substring(packageName, 0, StringUtils.lastIndexOf(packageName, '.')));

    return factoryBean;
}
 
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: AppConfig.java    From Spring-Framework-Essentials with MIT License 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {

    Properties props = new Properties();
    props.setProperty("hibernate.format_sql", String.valueOf(true));

    LocalContainerEntityManagerFactoryBean emf =
            new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource);
    emf.setPackagesToScan("com.oreilly.entities");
    emf.setJpaVendorAdapter(jpaVendorAdapter);
    emf.setJpaProperties(props);

    return emf;
}
 
Example #10
Source File: MySQLAutoconfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() throws IOException {
  final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  em.setDataSource(dataSource());
  em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
 
  final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  em.setJpaVendorAdapter(vendorAdapter);
  em.setJpaProperties(additionalProperties());
  return em;
}
 
Example #11
Source File: JpaTestContext.java    From OpERP with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
	emf.setDataSource(this.dataSource());
	emf.setJpaVendorAdapter(this.jpaVendorAdapter());
	emf.setPackagesToScan(packagesToScan);
	emf.setJpaProperties(this.hibernateProperties());
	return emf;
}
 
Example #12
Source File: JpaConfig.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
	emf.setDataSource(dataSource());
	emf.setPackagesToScan("com.hantsylabs.example.spring.model");
	emf.setPersistenceProvider(new HibernatePersistenceProvider());
	emf.setJpaProperties(jpaProperties());
	return emf;
}
 
Example #13
Source File: FhirServerConfigDstu3.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
@Override
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
    retVal.setPersistenceUnitName(HapiProperties.getPersistenceUnitName());

    try {
        retVal.setDataSource(myDataSource);
    } catch (Exception e) {
        throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
    }

    retVal.setJpaProperties(HapiProperties.getProperties());
    return retVal;
}
 
Example #14
Source File: DatabaseConfiguration.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
protected LocalContainerEntityManagerFactoryBean
          getLocalContainerEntityManagerFactoryBean(DataSource dataSource, EclipseLinkJpaVendorAdapter eclipseLinkJpaVendorAdapter,
                                                    String persistenceUnitName) {
    LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    localContainerEntityManagerFactoryBean.setPersistenceUnitName(persistenceUnitName);
    localContainerEntityManagerFactoryBean.setDataSource(dataSource);
    localContainerEntityManagerFactoryBean.setJpaVendorAdapter(eclipseLinkJpaVendorAdapter);
    localContainerEntityManagerFactoryBean.setPackagesToScan("com.sap.cloud.lm.sl");
    return localContainerEntityManagerFactoryBean;
}
 
Example #15
Source File: FhirServerConfigR5.java    From hapi-fhir-jpaserver-starter with Apache License 2.0 5 votes vote down vote up
@Override
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
    retVal.setPersistenceUnitName("HAPI_PU");

    try {
        retVal.setDataSource(myDataSource);
    } catch (Exception e) {
        throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
    }

    retVal.setJpaProperties(HapiProperties.getJpaProperties());
    return retVal;
}
 
Example #16
Source File: DbConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "com.baeldung.books.models" });
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    em.setJpaProperties(additionalProperties());
    return em;
}
 
Example #17
Source File: FirstDataSourceConfig.java    From learning-code with Apache License 2.0 5 votes vote down vote up
@Primary
@Bean(name = "firstEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(firstDataSource())
            .properties(getVendorProperties(firstDataSource()))
            .packages("com.wuwii.module.system.entity") //设置该数据源对应的实体类所在位置
            .persistenceUnit("firstPersistenceUnit")
            .build();
}
 
Example #18
Source File: H2JpaConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "com.baeldung.boot.domain" });
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    em.setJpaProperties(additionalProperties());
    return em;
}
 
Example #19
Source File: SpringWebConfig.java    From spring-mvc-react with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
	entityManagerFactoryBean.setDataSource(dataSource());
	entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
	entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROP_ENTITYMANAGER_PACKAGES_TO_SCAN));

	entityManagerFactoryBean.setJpaProperties(getHibernateProperties());

	return entityManagerFactoryBean;
}
 
Example #20
Source File: ReplicationDataSourceApplicationConfig.java    From replication-datasource with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("dataSource") DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
    emfb.setDataSource(dataSource);
    emfb.setPersistenceUnitName("replicationTest");
    JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    emfb.setJpaVendorAdapter(jpaVendorAdapter);
    return emfb;
}
 
Example #21
Source File: JpaConfig.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
	emf.setDataSource(dataSource());
	emf.setPackagesToScan("com.hantsylabs.example.spring.model");
	emf.setPersistenceProvider(new HibernatePersistenceProvider());
	emf.setJpaProperties(jpaProperties());
	return emf;
}
 
Example #22
Source File: EnableJpaStoresTest.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
	vendorAdapter.setDatabase(Database.HSQL);
	vendorAdapter.setGenerateDdl(true);

	LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
	factory.setJpaVendorAdapter(vendorAdapter);
	factory.setPackagesToScan(getClass().getPackage().getName());
	factory.setDataSource(dataSource());

	return factory;
}
 
Example #23
Source File: DataRepositoryConfiguration.java    From spring-data-jpa-entity-graph with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

  LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  factory.setDataSource(dataSource());
  factory.setMappingResources("META-INF/orm.xml");
  factory.setPackagesToScan("com.cosium.spring.data.jpa.entity.graph");

  HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
  jpaVendorAdapter.setDatabase(Database.H2);
  jpaVendorAdapter.setGenerateDdl(true);
  factory.setJpaVendorAdapter(jpaVendorAdapter);

  return factory;
}
 
Example #24
Source File: UsercenterApplication.java    From UserCenter with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setPackagesToScan("com.stalary.usercenter.data.entity");

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());
    return em;
}
 
Example #25
Source File: JpaConfig.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
	emf.setDataSource(dataSource());
	emf.setPackagesToScan("com.hantsylabs.example.spring.model");
	emf.setPersistenceProvider(new HibernatePersistenceProvider());
	emf.setJpaProperties(jpaProperties());
	return emf;
}
 
Example #26
Source File: JpaConfiguration.java    From example-ddd-with-spring-data-jpa with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    Map<String, Object> vendorProperties = getVendorProperties();
    return entityManagerFactoryBuilder().dataSource(dataSource())
                                        .packages("de.ck35.example.ddd.jpa")
                                        .properties(vendorProperties)
                                        .jta(false)
                                        .build();
}
 
Example #27
Source File: WebAppConfig.java    From SA47 with The Unlicense 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
	LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
	entityManagerFactoryBean.setDataSource(dataSource());
	entityManagerFactoryBean
			.setPersistenceProviderClass(HibernatePersistenceProvider.class);
	entityManagerFactoryBean
			.setPackagesToScan(env
					.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));

	entityManagerFactoryBean.setJpaProperties(hibProperties());

	return entityManagerFactoryBean;
}
 
Example #28
Source File: PrimaryJpaConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 获取主库实体管理工厂对象
 *
 * @param primaryDataSource 注入名为primaryDataSource的数据源
 * @param jpaProperties     注入名为primaryJpaProperties的jpa配置信息
 * @param builder           注入EntityManagerFactoryBuilder
 * @return 实体管理工厂对象
 */
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource, @Qualifier("primaryJpaProperties") JpaProperties jpaProperties, EntityManagerFactoryBuilder builder) {
    return builder
            // 设置数据源
            .dataSource(primaryDataSource)
            // 设置jpa配置
            .properties(jpaProperties.getProperties())
            // 设置实体包名
            .packages(ENTITY_PACKAGE)
            // 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
            .persistenceUnit("primaryPersistenceUnit").build();
}
 
Example #29
Source File: BaseApplication.java    From rqueue with Apache License 2.0 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  vendorAdapter.setGenerateDdl(true);
  LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  factory.setJpaVendorAdapter(vendorAdapter);
  factory.setPackagesToScan("com.github.sonus21.rqueue.test.entity");
  factory.setDataSource(dataSource());
  return factory;
}
 
Example #30
Source File: PersistenceConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "com.baeldung.persistence.model", "com.baeldung.springpagination.model" });

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    // vendorAdapter.set
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}