gpt4 book ai didi

java - 如何在play框架中使用多个实体管理器-使用spring data JPA?

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:03 24 4
gpt4 key购买 nike

我正在使用带有 Spring Data JPA 的 Play 框架。我需要连接到多个数据库,例如 db1、db2、db3。

持久性.xml

<persistence-unit name="db1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>db1</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>

</properties>
</persistence-unit>
<persistence-unit name="db2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>db2</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>

</properties>
</persistence-unit>
<persistence-unit name="db3" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>db3</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>

</properties>
</persistence-unit>

应用程序.conf

db.db1.driver=com.mysql.jdbc.Driver
db.db1.url="mysql://root:root@localhost/db1?zeroDateTimeBehavior=convertToNull"
db.db1.user="root"
db.db1.password="root"
db.db1.jndiName=db1
db.db1.logSql=true

db.db2.driver=com.mysql.jdbc.Driver
db.db2.url="mysql://root:root@localhost/db2?zeroDateTimeBehavior=convertToNull"
db.db2.user="root"
db.db2.password="root"
db.db2.jndiName=db2
db.db2.logSql=true

db.db3.driver=com.mysql.jdbc.Driver
db.db3.url="mysql://root:root@localhost/db3?zeroDateTimeBehavior=convertToNull"
db.db3.user="root"
db.db3.password="root"
db.db3.jndiName=db3

Spring 数据配置在 global.java 中定义,我从激活器创建的示例项目中获取它

@Configuration
@EnableJpaRepositories("models")
public static class SpringDataJpaConfiguration {

@Bean
public EntityManagerFactory entityManagerFactory() {
return Persistence.createEntityManagerFactory("db1");
}

@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}

@Bean
public JpaTransactionManager transactionManager() {
return new JpaTransactionManager();
}
}

现在,当我运行我的应用程序时,它会显示

[info] play - datasource [mysql://root:root@localhost/db1?zeroDateTimeBehavior=convertToNull] bound to JNDI as db1
[info] play - datasource [mysql://root:root@localhost/db2?zeroDateTimeBehavior=convertToNull] bound to JNDI as db2
[info] play - datasource [mysql://root:root@localhost/db3?zeroDateTimeBehavior=convertToNull] bound to JNDI as db3
[info] play - database [db1] connected at jdbc:mysql://localhost/db1?zeroDateTimeBehavior=convertToNull
[info] play - database [db2] connected at jdbc:mysql://localhost/db2?zeroDateTimeBehavior=convertToNull
[info] play - database [db3] connected at jdbc:mysql://localhost/db3?zeroDateTimeBehavior=convertToNull
[info] play - Application started (Dev)

现在我正在努力启用多个实体管理器,因为上述配置仅允许连接到 db1 数据库。我尝试在 global.java 中创建多个配置,但它会覆盖最后一个配置。请让我知道如何使用多实体管理器?

尝试使用以下链接加载多个数据源 Spring Boot, Spring Data JPA with multiple DataSources但仍然出现错误

play.api.UnexpectedException: Unexpected exception[UnsatisfiedDependencyException: Error creating bean with name 'attributeTypes' defined in file [/play-spring-data-jpa/target/scala-2.10/classes/controllers/catalog/AttributeTypes.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [models.repositories.data.AttributeTypeRepository]: Error creating bean with name 'attributeTypeRepository': Cannot create inner bean '(inner bean)#1b48832c' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1b48832c': Cannot resolve reference to bean 'dataEntityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.orm.jpa.JpaVendorAdapter models.repositories.data.DataConfig.jpaVendorAdapter; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.jpa.JpaVendorAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'attributeTypeRepository': Cannot create inner bean '(inner bean)#1b48832c' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1b48832c': Cannot resolve reference to bean 'dataEntityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.orm.jpa.JpaVendorAdapter models.repositories.data.DataConfig.jpaVendorAdapter; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.jpa.JpaVendorAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10-2.2.0.jar:2.2.0]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10-2.2.0.jar:2.2.0]
at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10-2.2.0.jar:2.2.0]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10-2.2.0.jar:2.2.0]
at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]

最佳答案

在 application.conf 文件中,为每个持久性单元添加一个新行,如下所示:

jpa.<name>=<persistence-unit-name>

就您而言:

jpa.default=db1
jpa.db2=db2
jpa.db3=db3

关于java - 如何在play框架中使用多个实体管理器-使用spring data JPA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37524142/

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