创建使用Java代码配置管理MongoDb连接的Jpa EntityManager

时间:2019-01-08 20:53:22

标签: java hibernate spring-boot hibernate-ogm

我需要定义两个不同的持久性单元,一个用于mysql db,另一个用于mongodb,我试图用Spring的@Configuration创建将管理Mongo的entityManager,但是应用程序在Spring的引导程序中失败,如下所示错误:在类路径资源[com / intraway / qx / utilities / config / SpringDataJpaMongoConfiguration.class]中创建名称为'mongoEntityManager'的bean时出错:调用init方法失败;嵌套的异常是java.lang.NullPointerException

    Java config Class
@Configuration
@EnableJpaRepositories(basePackages = { "com.intraway.qx.utilities.models.repositories.mongoRepository"},
        entityManagerFactoryRef = "mongoEntityManager",
        transactionManagerRef = "mongoTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaMongoConfiguration {

    @Bean(name = "mongoEntityManager")
    public LocalContainerEntityManagerFactoryBean  mongoEntityManager() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("javax.persistence.transactionType", "resource_local");
        properties.put("hibernate.ogm.datastore.provider","mongodb");
        properties.put("hibernate.ogm.datastore.host","localhost");
        properties.put("hibernate.ogm.datastore.port", "27017");
        properties.put("hibernate.ogm.datastore.database", "qx_utils");

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPackagesToScan("com.intraway.qx.utilities.models");
        em.setPersistenceUnitName("persistence.mongo");
        em.setJpaPropertyMap(properties);
        em.setPersistenceProviderClass(HibernateOgmPersistence.class);
        return em;
    }
    @Bean(name = "mongoTransactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(mongoEntityManager().getObject());
        return transactionManager;
    }
}

POM.xml          http://maven.apache.org/xsd/maven-4.0.0.xsd“>         4.0.0

    <groupId>com.intraway.qx</groupId>
    <artifactId>QX-AOS-Utilities</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>QX-AOS-Utilities</name>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repositories -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-mongodb</artifactId>
            <version>5.3.1.Final</version>
        </dependency>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我需要以编程方式配置DB连接(不通过application.properties文件)。

1 个答案:

答案 0 :(得分:0)

实际上,我不知道您是否在MySQL DB配置中使用了@Primary注释。 但是,如果您使用的是两个数据源,则必须将其中一个设置为主。

即使使用了属性,您也可以按照本教程进行操作,但是您也可以将它们加入配置类。

https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

4。为名为“ foo”的Oracle数据库创建一个配置类 “ FooDbConfig.java”

打包com.foobar;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "entityManagerFactory",
  basePackages = { "com.foobar.foo.repo" }
)
public class FooDbConfig {

@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}