如何将第二个数据源添加到Micronaut项目?

时间:2019-05-24 16:17:57

标签: java hibernate jpa jdbc micronaut

我有一个Java Micronaut项目,想添加第二个数据源。只要添加第二个数据源,我的代码就可以正常工作。 documentation刚刚说过,我必须在配置中以新名称添加新源。我在做什么错以及如何使用第二个数据库。

我的application.yaml

---
micronaut:
  application:
    name: transfer-service

---
datasources:
  default:
    url: jdbc:postgresql://XXX.XXX.XXX.XXX:XXXX/t4dserver
    driverClassName: org.postgresql.Driver
    username: postgres
    password: password
  germany:
    url: jdbc:postgresql://YYY.YYY.YYY.YYY:YYYY/t4dserver
    driverClassName: org.postgresql.Driver
    username: postgres
    password: password
jpa:
  default:
    properties:
      hibernate:
        hbm2ddl:
          auto: none

添加“德国”数据源时出现的错误:

ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.boot.MetadataSources]

Message: Missing bean argument value: jpaConfiguration
Path Taken: SessionFactory.hibernateSessionFactory([SessionFactoryBuilder sessionFactoryBuilder]) --> SessionFactoryBuilder.hibernateSessionFactoryBuilder([MetadataSources metadataSources],ValidatorFactory validatorFactory) --> MetadataSources.hibernateMetadataSources([JpaConfiguration jpaConfiguration],StandardServiceRegistry standardServiceRegistry)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.boot.MetadataSources]

Message: Missing bean argument value: jpaConfiguration
Path Taken: SessionFactory.hibernateSessionFactory([SessionFactoryBuilder sessionFactoryBuilder]) --> SessionFactoryBuilder.hibernateSessionFactoryBuilder([MetadataSources metadataSources],ValidatorFactory validatorFactory) --> MetadataSources.hibernateMetadataSources([JpaConfiguration jpaConfiguration],StandardServiceRegistry standardServiceRegistry)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1174)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:236)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:2313)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:199)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:187)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:69)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:288)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:274)
        at transfer.service.Application.main(Application.java:8)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [org.hibernate.boot.MetadataSources]

Message: Missing bean argument value: jpaConfiguration
Path Taken: SessionFactory.hibernateSessionFactory([SessionFactoryBuilder sessionFactoryBuilder]) --> SessionFactoryBuilder.hibernateSessionFactoryBuilder([MetadataSources metadataSources],ValidatorFactory validatorFactory) --> MetadataSources.hibernateMetadataSources([JpaConfiguration jpaConfiguration],StandardServiceRegistry standardServiceRegistry)
        at io.micronaut.context.AbstractParametrizedBeanDefinition.build(AbstractParametrizedBeanDefinition.java:99)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:201)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1494)
        at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2163)
        at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:1849)
        at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:1829)
        at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:997)
        at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:982)
        at io.micronaut.configuration.hibernate.jpa.$EntityManagerFactoryBean$HibernateSessionFactoryBuilderDefinition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:205)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1494)
        at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2163)
        at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:1849)
        at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:1829)
        at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:997)
        at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:982)
        at io.micronaut.configuration.hibernate.jpa.$EntityManagerFactoryBean$HibernateSessionFactoryDefinition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:205)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1494)
        at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2163)
        at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:1761)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1172)
        ... 8 common frames omitted

1 个答案:

答案 0 :(得分:1)

写我的评论作为答案

我认为您需要将第二个数据源添加到jpa部分中,这样:

jpa:
   default:
      properties:
         hibernate:
            hbm2ddl:
               auto: none
   germany:
      properties:
         hibernate:
            hbm2ddl:
               auto: none