Java Code Examples for org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder

The following examples show how to use org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder. These examples are extracted from open source projects. 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 Project: txle   Source File: SecondaryConfig.java    License: Apache License 2.0 6 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 2
Source Project: database-rider   Source File: UserDBConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Primary
@Bean(name = "userEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder builder, @Qualifier("userDataSource") DataSource userDataSource) {

    Map<String, Object> properties = new HashMap<>();
    properties.put("hibernate.hbm2ddl.auto",
            env.getProperty("spring.jpa.hibernate.ddl-auto"));
    properties.put("hibernate.dialect",
            env.getProperty("spring.jpa.properties.hibernate.dialect"));
    return builder
            .dataSource(userDataSource)
            .properties(properties)
            .packages(UserRepository.class.getPackage().getName())
            .persistenceUnit("userPU")
            .build();
}
 
Example 3
@Bean(name = "cardEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean cardEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(cardDataSource())
            .packages(Card.class)
            .build();
}
 
Example 4
@Primary
@Bean(name = "memberEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean memberEntityManagerFactory(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(memberDataSource())
            .packages(Member.class)
            .build();
}
 
Example 5
@Bean(name = "cardHolderEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(cardholderDataSource())
            .packages(CardHolder.class)
            .build();
}
 
Example 6
Source Project: txle   Source File: PrimaryConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource, EntityManagerFactoryBuilder builder) {
    return builder.dataSource(primaryDataSource)
            .properties(jpaProperties.getProperties())
            .packages("com.actionsky.txle.grpc.entity")
            .persistenceUnit("primaryPersistenceUnit")
            .build();
}
 
Example 7
Source Project: spring-cloud   Source File: RepositoryPrimaryConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "", name = "entityManagerPrimary")
@Primary
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactoryPrimary(builder)
            .getObject()
            .createEntityManager();
}
 
Example 8
Source Project: spring-cloud   Source File: RepositoryPrimaryConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "", name = "entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
    return builder.dataSource(r_ds)
            .properties(getVendorProperties(r_ds))
            .packages("cn.cib.entity.read")
            .persistenceUnit("primaryPersistenceUnit")
            .build();
}
 
Example 9
Source Project: spring-cloud   Source File: RepositorySecondaryConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "", name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
    return builder.dataSource(w_ds)
            .properties(getVendorProperties(w_ds))
            .packages("cn.cib.entity.write")
            .persistenceUnit("secondaryPersistenceUnit").build();
}
 
Example 10
Source Project: spring-boot-multi-tenancy   Source File: Application.java    License: MIT License 5 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factory, DataSource dataSource, JpaProperties properties) {
  HibernateSettings settings = new HibernateSettings();
  settings.ddlAuto("create-drop");
  Map<String, Object> jpaProperties = new HashMap<>(properties.getHibernateProperties(settings));
  jpaProperties.put("hibernate.ejb.interceptor", hibernateInterceptor());
  return factory.dataSource(dataSource).packages("com.example").properties(jpaProperties).build();
}
 
Example 11
Source Project: md_blockchain   Source File: JpaConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Autowired
@Bean
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(@Qualifier(value =
        "EmbeddeddataSource") DataSource dataSource, EntityManagerFactoryBuilder builder) {
    return builder.dataSource(dataSource)
            .packages("com.mindata.blockchain.core.model")
            .properties(getVendorProperties(dataSource))
            .build();
}
 
Example 12
Source Project: learning-code   Source File: SecondDataSource.java    License: Apache License 2.0 5 votes vote down vote up
@Primary
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(dataSource)
            .properties(getVendorProperties(dataSource))
            .packages("com.wuwii.module.user.entity") //设置实体类所在位置
            .persistenceUnit("secondPersistenceUnit")
            .build();
}
 
Example 13
Source Project: learning-code   Source File: SecondDataSourceConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(secondDataSource())
            .properties(getVendorProperties(secondDataSource()))
            .packages("com.wuwii.module.user.entity") //设置该数据源锁对应的实体类所在的位置
            .persistenceUnit("secondPersistenceUnit")
            .build();
}
 
Example 14
Source Project: learning-code   Source File: FirstDataSourceConfig.java    License: 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 15
Source Project: springboot-security-wechat   Source File: AuditDsConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 实体管理对象
 * @param builder 由spring注入这个对象,首先根据type注入(多个就取声明@Primary的对象),否则根据name注入
 * @return
 */
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(firstDataSource())
            .packages("com.luotuo.entity")
            .persistenceUnit("firstDs")
            .build();
}
 
Example 16
Source Project: springboot-security-wechat   Source File: UserDsConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 实体管理对象
 * @param builder 由spring注入这个对象,首先根据type注入(多个就取声明@Primary的对象),否则根据name注入
 * @return
 */
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(secondDataSource())
            .packages("com.luotuo.user.entity")
            .persistenceUnit("secondDs")
            .build();
}
 
Example 17
Source Project: SpringBoot-Study   Source File: ReadDataSourceConfig1.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * EntityManagerFactory类似于Hibernate的SessionFactory,mybatis的SqlSessionFactory
 * 总之,在执行操作之前,我们总要获取一个EntityManager,这就类似于Hibernate的Session,
 * mybatis的sqlSession.
 * @return
 */
@Bean(name = "readEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean readEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(readDruidDataSource)
            .properties(jpaProperties.getProperties())
            .packages("com.lengchuan.springBoot.jpa.entity") //设置实体类所在位置
            .persistenceUnit("readPersistenceUnit")//持久化单元名称
            .build();
    //.getObject();
}
 
Example 18
Source Project: SpringBoot-Study   Source File: WriteDataSourceConfig1.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 我们通过LocalContainerEntityManagerFactoryBean来获取EntityManagerFactory实例
 * @return
 */
@Bean(name = "writeEntityManagerFactoryBean")
@Primary
public LocalContainerEntityManagerFactoryBean writeEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(writeDruidDataSource)
            .properties(jpaProperties.getProperties())
            .packages("com.lengchuan.springBoot.jpa.entity") //设置实体类所在位置
            .persistenceUnit("writePersistenceUnit")
            .build();
    //.getObject();//不要在这里直接获取EntityManagerFactory
}
 
Example 19
public EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(DataSource dataSource) {
	JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter(dataSource);
	EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
			jpaVendorAdapter, properties.getProperties(),
			this.persistenceUnitManager);
	return builder;
}
 
Example 20
Source Project: loc-framework   Source File: LocJpaConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
    JpaVendorAdapter jpaVendorAdapter,
    ObjectProvider<PersistenceUnitManager> persistenceUnitManager) {
  return new EntityManagerFactoryBuilder(
      jpaVendorAdapter, jpaProperties.getProperties(),
      persistenceUnitManager.getIfAvailable());
}
 
Example 21
Source Project: spring-boot-demo   Source File: SecondJpaConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 获取主库实体管理工厂对象
 *
 * @param secondDataSource 注入名为secondDataSource的数据源
 * @param jpaProperties    注入名为secondJpaProperties的jpa配置信息
 * @param builder          注入EntityManagerFactoryBuilder
 * @return 实体管理工厂对象
 */
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("secondDataSource") DataSource secondDataSource, @Qualifier("secondJpaProperties") JpaProperties jpaProperties, EntityManagerFactoryBuilder builder) {
    return builder
            // 设置数据源
            .dataSource(secondDataSource)
            // 设置jpa配置
            .properties(jpaProperties.getProperties())
            // 设置实体包名
            .packages(ENTITY_PACKAGE)
            // 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
            .persistenceUnit("secondPersistenceUnit").build();
}
 
Example 22
Source Project: spring-boot-demo   Source File: PrimaryJpaConfig.java    License: 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 23
Source Project: SuperBoot   Source File: DataSourceTransactionConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 我们通过LocalContainerEntityManagerFactoryBean来获取EntityManagerFactory实例
 *
 * @return
 */
@Bean(name = "dataEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(dataSource)
            .properties(jpaProperties.getProperties())
            //设置实体类所在位置
            .packages("org.superboot.entity.jpa")
            .persistenceUnit("dataPersistenceUnit")
            .build();
}
 
Example 24
Source Project: sample-boot-micro   Source File: OrmRepository.java    License: MIT License 5 votes vote down vote up
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(String name, final DataSource dataSource) {
    EntityManagerFactoryBuilder emfBuilder = new EntityManagerFactoryBuilder(
            vendorAdapter(), getProperties(), null);
    Builder builder = emfBuilder
            .dataSource(dataSource)
            .persistenceUnit(name)
            .properties(hibernate.determineHibernateProperties(getProperties(), new HibernateSettings()))
            .jta(false);
    if (ArrayUtils.isNotEmpty(annotatedClasses)) {
        builder.packages(annotatedClasses);
    } else {
        builder.packages(packageToScan);
    }
    return builder.build();
}
 
Example 25
Source Project: database-rider   Source File: CompanyDBConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "companyEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder builder, @Qualifier("companyDataSource") DataSource companyDataSource) {
    Map<String, Object> properties = new HashMap<>();
    properties.put("hibernate.hbm2ddl.auto",
            env.getProperty("spring.jpa.hibernate.ddl-auto"));
    properties.put("hibernate.dialect",
            env.getProperty("spring.jpa.properties.hibernate.dialect"));
    return builder
            .dataSource(companyDataSource)
            .properties(properties)
            .packages(CompanyRepository.class.getPackage().getName())
            .persistenceUnit("companyPU")
            .build();
}
 
Example 26
Source Project: sample-boot-hibernate   Source File: OrmRepository.java    License: MIT License 5 votes vote down vote up
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(String name, final DataSource dataSource) {
    EntityManagerFactoryBuilder emfBuilder = new EntityManagerFactoryBuilder(
            vendorAdapter(), getProperties(), null);
    Builder builder = emfBuilder
            .dataSource(dataSource)
            .persistenceUnit(name)
            .properties(hibernate.determineHibernateProperties(getProperties(), new HibernateSettings()))
            .jta(false);
    if (ArrayUtils.isNotEmpty(annotatedClasses)) {
        builder.packages(annotatedClasses);
    } else {
        builder.packages(packageToScan);
    }
    return builder.build();
}
 
Example 27
Source Project: ddd-java   Source File: EntityTestSupport.java    License: MIT License 5 votes vote down vote up
protected void setupEntityManagerFactory() {
    DataSource ds = EntityTestFactory.dataSource();
    Map<String, String> props = new HashMap<>();
    props.put(AvailableSettings.HBM2DDL_AUTO, "create-drop");
    Builder builder = new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), props, null)
            .dataSource(ds)
            .jta(false);
    if (!targetEntities.isEmpty()) {
        builder.packages(targetEntities.toArray(new Class<?>[0]));
    }
    LocalContainerEntityManagerFactoryBean emfBean = builder.build();
    emfBean.afterPropertiesSet();
    emf = emfBean.getObject();
    txm = new JpaTransactionManager(emf);
}
 
Example 28
Source Project: spring-cloud   Source File: RepositoryPrimaryConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "", name = "transactionManagerPrimary")
@Primary
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
 
Example 29
Source Project: spring-cloud   Source File: RepositorySecondaryConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "", name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
 
Example 30
Source Project: spring-cloud   Source File: RepositorySecondaryConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "", name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}