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

The following examples show how to use org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder. 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: 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 #2
Source File: UserDBConfig.java    From database-rider with 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
Source File: SecondJpaConfig.java    From spring-boot-demo with 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 #4
Source File: SecondDataSource.java    From learning-code with 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 #5
Source File: SecondDataSourceConfig.java    From learning-code with 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 #6
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 #7
Source File: AuditDsConfig.java    From springboot-security-wechat with 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 #8
Source File: UserDsConfig.java    From springboot-security-wechat with 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 #9
Source File: ReadDataSourceConfig1.java    From SpringBoot-Study with 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 #10
Source File: WriteDataSourceConfig1.java    From SpringBoot-Study with 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 #11
Source File: EntityManagerFactoryServiceImpl.java    From multitenant with Apache License 2.0 5 votes vote down vote up
public EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(DataSource dataSource) {
	JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter(dataSource);
	EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
			jpaVendorAdapter, properties.getProperties(),
			this.persistenceUnitManager);
	return builder;
}
 
Example #12
Source File: LocJpaConfiguration.java    From loc-framework with 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 #13
Source File: MemberDataSourceConfiguration.java    From sfg-blog-posts with GNU General Public License v3.0 5 votes vote down vote up
@Primary
@Bean(name = "memberEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean memberEntityManagerFactory(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(memberDataSource())
            .packages(Member.class)
            .build();
}
 
Example #14
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 #15
Source File: DataSourceTransactionConfig.java    From SuperBoot with 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 #16
Source File: OrmRepository.java    From sample-boot-micro with 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 #17
Source File: CompanyDBConfig.java    From database-rider with 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 #18
Source File: OrmRepository.java    From sample-boot-hibernate with 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 #19
Source File: EntityTestSupport.java    From ddd-java with 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 #20
Source File: JpaConfiguration.java    From md_blockchain with 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 #21
Source File: CardDataSourceConfiguration.java    From sfg-blog-posts with GNU General Public License v3.0 5 votes vote down vote up
@Bean(name = "cardEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean cardEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(cardDataSource())
            .packages(Card.class)
            .build();
}
 
Example #22
Source File: CardHolderDataSourceConfiguration.java    From sfg-blog-posts with GNU General Public License v3.0 5 votes vote down vote up
@Bean(name = "cardHolderEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(cardholderDataSource())
            .packages(CardHolder.class)
            .build();
}
 
Example #23
Source File: PrimaryConfig.java    From txle with 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 #24
Source File: RepositoryPrimaryConfig.java    From spring-cloud with 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 #25
Source File: RepositoryPrimaryConfig.java    From spring-cloud with 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 #26
Source File: RepositorySecondaryConfig.java    From spring-cloud with 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 #27
Source File: Application.java    From spring-boot-multi-tenancy with 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 #28
Source File: FirstDataSourceConfig.java    From learning-code with Apache License 2.0 4 votes vote down vote up
@Primary
@Bean(name = "firstTransactionManager")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
 
Example #29
Source File: SecondDataSource.java    From learning-code with Apache License 2.0 4 votes vote down vote up
@Primary
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
 
Example #30
Source File: SecondDataSourceConfig.java    From learning-code with Apache License 2.0 4 votes vote down vote up
@Bean(name = "secondEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}