使用Spring Boot JPA配置多个数据源

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Spring Boot JPA配置多个数据源相关的知识,希望对你有一定的参考价值。

根据一个教程,我创建了两个配置类,它们具有相同数据库的不同模式,并且可以在模型类中不使用@onetomany和@manytoone批注,但是使用这些批注时可以完美运行,当我尝试运行该应用程序时,我得到以下信息错误。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamicHouseEntityManagerFactory' defined in class path resource [com/oroprise/auth/app/config/DynamicHouseDbConfig.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.oroprise.auth.app.securedynamic.model.RadioCheckboxelements.house_dynamicform in com.oroprise.auth.app.securedynamic.model.DynamicHouse.labelnames
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:606) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:594) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1226) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:291) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:102) ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80) ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44) ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]
    at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:303) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1681) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1433) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:211) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:174) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:169) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:154) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:86) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:253) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:227) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:53) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5135) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_171]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source) ~[?:1.8.0_171]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_171]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source) ~[?:1.8.0_171]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:932) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:459) ~[tomcat-embed-core-9.0.24.jar:9.0.24]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:105) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:416) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:180) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    ... 13 more

application.properties

        spring.jpa.database=default
        spring.datasource.platform=postgres
        # PostgreSQL DB - "primary"
        spring.datasource.driver-class-name=org.postgresql.Driver
        spring.datasource.jdbc-url= jdbc:postgresql://localhost:5433/SurveyDB?currentSchema=securesurvey
        spring.datasource.username=postgres
        spring.datasource.password=password
        # PostgreSQL DB - "secondary"
        spring.dynamicform.datasource.driver-class-name=org.postgresql.Driver
        spring.dynamicform.datasource.jdbc-url= jdbc:postgresql://localhost:5433/SurveyDB?currentSchema=securedynamic
        spring.dynamicform.datasource.username=postgres
        spring.dynamicform.datasource.password=password
        # ===============================
        # = JPA / HIBERNATE
        # ===============================
        spring.jpa.show-sql = true
        spring.jpa.hibernate.ddl-auto = update
        spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
        spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
        spring.jpa.properties.hibernate.format_sql = true

DynamicHouse.java

@Entity
        @Table(name = "house_dynamicform")
        public class DynamicHouse implements Serializable 
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name = "id")
            private Long id;
            @NotNull
            @Size(max = 100)
            @Column(name = "column_name")
            private String columnName;
            @NotNull
            @Size(max = 100)
            @Column(name = "data_type")
            private String dataType;
            @NotNull
            @Size(max = 100)
            @Column(name = "elements")
            private String elements;
            @NotNull
            @Size(max = 5)
            @Column(name = "foreign_key")
            private String foreignKey;
            @NotNull
            @Size(max = 5)
            @Column(name = "is_default")
            private String isDefault;
            @NotNull
            @Size(max = 5)
            @Column(name = "is_null")
            private String isNull;
            @NotNull
            @Size(max = 50)
            @Column(name = "char_length")
            private String characterLength;
            @NotNull
            @Column(name = "table_id")
            private Long tableId;
            @NotNull
            @Size(max = 250)
            @Column(name = "displayname")
            private String displayName;
            @NotNull
            @Size(max = 100)
            @Column(name = "proj_zone")
            private String projZone;
            @NotNull
            @Size(max = 100)
            @Column(name = "field_name")
            private String fieldName;
            @NotNull
            @Size(max = 100)
            @Column(name = "fieldsequence")
            private String fieldSequence;
            @Column(name = "location_date")
            @Temporal(TemporalType.TIMESTAMP)
            private Date locationDate;
            @NotNull
            @Size(max = 100)
            @Column(name = "createdby")
            private String createdBy;
            @Transient
            private String locatDate;

            @OneToMany(cascade = CascadeType.ALL, mappedBy = "house_dynamicform", fetch = FetchType.EAGER)
            private List<RadioCheckboxelements> labelnames = new ArrayList<>();

            public DynamicHouse() 
            

            public DynamicHouse(Long id, @NotNull @Size(max = 100) String columnName, @NotNull @Size(max = 100) String dataType,
                    @NotNull @Size(max = 100) String elements, @NotNull @Size(max = 5) String foreignKey,
                    @NotNull @Size(max = 5) String isDefault, @NotNull @Size(max = 5) String isNull,
                    @NotNull @Size(max = 50) String characterLength, @NotNull Long tableId,
                    @NotNull @Size(max = 250) String displayName, @NotNull @Size(max = 100) String projZone,
                    @NotNull @Size(max = 100) String fieldName, @NotNull @Size(max = 100) String fieldSequence,
                    Date locationDate, @NotNull @Size(max = 100) String createdBy, String locatDate,
                    List<RadioCheckboxelements> labelnames) 
                super();
                this.id = id;
                this.columnName = columnName;
                this.dataType = dataType;
                this.elements = elements;
                this.foreignKey = foreignKey;
                this.isDefault = isDefault;
                this.isNull = isNull;
                this.characterLength = characterLength;
                this.tableId = tableId;
                this.displayName = displayName;
                this.projZone = projZone;
                this.fieldName = fieldName;
                this.fieldSequence = fieldSequence;
                this.locationDate = locationDate;
                this.createdBy = createdBy;
                this.locatDate = locatDate;
                this.labelnames = labelnames;
            
            //getter //setter
        

RadioCheckboxelements.java

@Entity
@Table(name = "radio_checkbox_elements")
public class RadioCheckboxelements implements Serializable 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "rcid")
    private Long rcid;
    @NotNull
    @Size(max = 100)
    @Column(name = "lablenames")
    private String labelnames;
    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "dynamichouseId", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private DynamicHouse dynamichouse;

    public RadioCheckboxelements() 
        super();
    

    public RadioCheckboxelements(Long rcid, @NotNull @Size(max = 100) String labelnames) 
        super();
        this.rcid = rcid;
        this.labelnames = labelnames;
    
    //getter //setter

PrimaryDbConfig.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager", basePackages = 
        "com.oroprise.auth.app.repository" )
public class PrimaryDbConfig 

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

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) 
        return builder.dataSource(dataSource).packages("com.oroprise.auth.app.model").persistenceUnit("User")
            .properties(singletonMap("hibernate.hbm2ddl.auto", "update"))
                .build();
    

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager primaryTransactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) 
        return new JpaTransactionManager(entityManagerFactory);
    


DynamicHouseDbConfig.java

@Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(entityManagerFactoryRef = "dynamicHouseEntityManagerFactory",
        transactionManagerRef = "dynamicHouseTransactionManager", basePackages = "com.oroprise.auth.app.securedynamic.repository")
    public class DynamicHouseDbConfig 

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

      @Bean(name = "dynamicHouseEntityManagerFactory")
      public LocalContainerEntityManagerFactoryBean dynamicHouseEntityManagerFactory(
          EntityManagerFactoryBuilder builder, @Qualifier("dynamicHouseDataSource") DataSource dataSource) 
        return builder.dataSource(dataSource).packages("com.oroprise.auth.app.securedynamic.model").persistenceUnit("DynamicHouse")
                .properties(singletonMap("hibernate.hbm2ddl.auto", "update"))
                    .build();
      

      @Bean(name = "dynamicHouseTransactionManager")
      public PlatformTransactionManager dynamicHouseTransactionManager(
          @Qualifier("dynamicHouseEntityManagerFactory") EntityManagerFactory dynamicHouseEntityManagerFactory) 
        return new JpaTransactionManager(dynamicHouseEntityManagerFactory);
      
    

DynamicFormController.java

@RestController
@CrossOrigin(origins = "http://oro.com")
public class DynamicFormController 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public DynamicFormService dynamicFormService;

    @RequestMapping(value = "/forms/houseform/getdetails/id", method = RequestMethod.GET)
    public ResponseEntity<?> fetchDynamicHouseHoldFormValues(@PathVariable(value="id") int id) 
        logger.info("DynamicFormController fetchDynamicHouseHoldFormValues() is invoked : " +id);
        SedclDetailsResponse stringResponse = null;
        try 
            List<DynamicHouse> list = dynamicFormService.fetchDynamicHouseData(id);
            if(list!=null) 
                stringResponse = new SedclDetailsResponse("True", list);
            
         catch (Exception e) 
            logger.error("Error in fetchDynamicFormValues() of DynamicFormController :- ", e);
            stringResponse = new SedclDetailsResponse("False", "Oops! Something went wrong, please try again.");
        
        return ResponseEntity.ok(stringResponse);
    

DynamicHouseRepository.java

@Repository("dynamicHouseRepository")
    public interface DynamicHouseRepository extends JpaRepository<DynamicHouse, Long> 

        @Query(value = "select * from house_dynamicform where table_id = ?1 order by fieldsequence asc", nativeQuery = true)
        public List<DynamicHouse> fetchDetails(long id);
    

RadioCheckboxRepository.java

@Repository("radioCheckboxRepository")
    public interface RadioCheckboxRepository extends JpaRepository<RadioCheckboxelements, Long> 

        @Query(value = "select * from radio_checkbox_elements where huse_id = ?1", nativeQuery = true)
        public List<RadioCheckboxelements> fetchLabelDetails(long id);
    

我已经尝试解决这个问题10多个小时了:(。预先感谢您的帮助。

答案

您可以尝试更新您的DynamicHouse实体,并查看它是否有效。

@OneToMany(cascade = CascadeType.ALL, mappedBy = "dynamichouse", fetch = FetchType.EAGER) 
private List<RadioCheckboxelements> labelnames = new ArrayList<>();

以上是关于使用Spring Boot JPA配置多个数据源的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring Boot 自动装配具有 jpa 和非 jpa 特征的多个数据源

Spring Boot Jpa多数据源配置

如何通过使用 JPA + Hibernate 和 Spring-boot 在一个数据库中使用多个模式?

多个数据库使用相同的域模型和 Spring Boot 和 Spring Data JPA

spring boot JPA 和 数据库连接

Spring Boot 整合 JPA 使用多个数据源