gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:24:12 28 4
gpt4 key购买 nike

我需要定义两个不同的持久单元,一个用于 mysql 数据库,另一个用于 mongodb,我尝试使用 Spring 的 @Configuration 创建将管理 Mongo 的实体管理器,但应用程序在 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>

我需要以编程方式配置数据库连接(而不是通过 application.properties 文件),这是一项要求。

最佳答案

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

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

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

<强>4。为 Oracle 数据库“foo”创建一个名为“foo”的配置类“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();
}

关于java - 使用java代码配置创建管理MongoDb连接的Jpa EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54099528/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com