2017-02-07 5 views
1

Essayer de configuration double source de données avec démarrage de printemps, mais il semble beaucoup plus difficile alors première pensée,multiples datasource avec botte ressort JPA

a essayé de suivre des tonnes de didacticiels et guides, mais continuer à obtenir l'erreur avec mes mises en pension.

propriétés d'application

# Primary DataSource 
datasource.primary.url=url 
datasource.primary.username=user 
datasource.primary.password=pw 
datasource.primary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 

# Secondary DataSource 
datasource.secondary.url=url 
datasource.secondary.username=user 
datasource.secondary.password=pw 
datasource.secondary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 

# hibernate strategy (create/update) 
spring.jpa.hibernate.ddl-auto=update 

# name strategy 
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 

première configuration db

package com.anders.cphbusiness.db; 

    import org.springframework.beans.factory.annotation.Qualifier; 
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 
    import org.springframework.boot.context.properties.ConfigurationProperties; 
    import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.Configuration; 
    import org.springframework.context.annotation.Primary; 
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
    import org.springframework.orm.jpa.JpaTransactionManager; 
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
    import org.springframework.transaction.PlatformTransactionManager; 
    import org.springframework.transaction.annotation.EnableTransactionManagement; 

    import javax.persistence.EntityManagerFactory; 
    import javax.persistence.PersistenceContext; 
    import javax.sql.DataSource; 

    @Configuration 
    @EnableTransactionManagement 
    @EnableJpaRepositories(
      basePackages = {"com.anders.cphbusiness.repositories"}, 
      entityManagerFactoryRef = "primaryEM") 

    public class LoadDataSourceConfig { 
     @Bean(name = "primaryDS") 
     @Primary 
     @ConfigurationProperties(prefix = "datasource.primary") 
     public DataSource loadingDataSource() { 
      return DataSourceBuilder.create().build(); 
     } 

     @PersistenceContext(unitName = "primaryPU") 
     @Primary 
     @Bean(name = "primaryEM") 
     public LocalContainerEntityManagerFactoryBean loadingEntityManagerFactory(
       EntityManagerFactoryBuilder builder, @Qualifier("primaryDS") DataSource primaryDS) { 
      return builder 
        .dataSource(primaryDS) 
        .persistenceUnit("primaryPU") 
        .packages("com.anders.cphbusiness.entitiesModel") 
        .build(); 
     } 

     @Primary 
     @Bean(name = "primaryTM") 
     public PlatformTransactionManager transactionManager(
       @Qualifier("primaryEM") EntityManagerFactory entityManagerFactory) { 
      return new JpaTransactionManager(entityManagerFactory); 
     } 

    } 

seconde configuration db

package com.anders.cphbusiness.db; 

import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 
import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.transaction.PlatformTransactionManager; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

import javax.persistence.EntityManagerFactory; 
import javax.persistence.PersistenceContext; 
import javax.sql.DataSource; 

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(
     basePackages = "com.anders.cphbusiness.secondRepo", 
     entityManagerFactoryRef = "secondaryEM", 
     transactionManagerRef = "secondaryTransactionManager") 

public class StoreDataSourceConfig { 

    @Bean(name = "secondaryDS") 
    @ConfigurationProperties(prefix = "datasource.secondary") 
    public DataSource storingDataSource() { 
     return DataSourceBuilder.create().build(); 
    } 

    @PersistenceContext(unitName = "secondaryPU") 
    @Bean(name = "secondaryEM") 
    public LocalContainerEntityManagerFactoryBean storingEntityManagerFactory(
      EntityManagerFactoryBuilder builder, @Qualifier("secondaryDS") DataSource secondaryDS) { 
     return builder 
       .dataSource(secondaryDS) 
       .packages("com.anders.cphbusiness.storingModel") 
       .persistenceUnit("secondaryPU") 
       .build(); 
    } 

    @Bean(name = "secondaryTM") 
    public PlatformTransactionManager secondaryTransactionManager(
      @Qualifier("secondaryEM") EntityManagerFactory secondaryEM) { 
     return new JpaTransactionManager(secondaryEM); 
    } 
} 

exemples de mise en pension

package com.anders.cphbusiness.repositories; 

import com.anders.cphbusiness.entitiesModel.WagerBoard; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 

import javax.persistence.PersistenceContext; 

@Repository 
@PersistenceContext(name = "primaryEM") 
public interface WagerBoardRepo extends JpaRepository<WagerBoard, String> { 

} 

storeDbEntRepo:

package com.anders.cphbusiness.secondRepo; 

import com.anders.cphbusiness.entitiesModel.WagerBoard; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 

import javax.persistence.PersistenceContext; 

@Repository 
@PersistenceContext(name = "secondaryEM") 
public interface StoreDbEntRepo extends JpaRepository<WagerBoard, String> { 

} 

l'erreur

2017-02-07 10:57:54.222 ERROR 7176 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storeDbEntRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.anders.cphbusiness.entitiesModel.WagerBoard 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at com.anders.cphbusiness.DsRngCheckerApplication.main(DsRngCheckerApplication.java:18) [main/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na] 
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.anders.cphbusiness.entitiesModel.WagerBoard 
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final] 
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.0.RELEASE.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.0.RELEASE.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.0.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    ... 21 common frames omitted 
+0

Où est votre haricot storeDbEntRepo? Peut-on le poster? –

+0

@CletoGadelha ajouté le repo –

Répondre

1

Votre principal problème est: not a managed type: class com.anders.cphbusiness.entitiesModel.WagerBoard

Votre StoreDbEntRepo utilise "secondaryEM", mais utilise entité WagerBoard pour créer le repo.

L'entité WagerBoard appartient au paquet entitiesModel. Si vous souhaitez utiliser cette entité dans votre StoreDbEntRepo, votre besoin de scanner le paquet approprié dans votre EntityManager, faire une mise à jour de

.packages("com.anders.cphbusiness.storingModel") 

à

.packages("com.anders.cphbusiness.storingModel","com.anders.cphbusiness.entitiesModel"). 

Mais si votre ne le font pas, utilisez un entité qui appartient au package storingModel pour créer le StoreDbEntRepo.

+0

entitiesModel est les entités pour db primaire et storageModel est pour db secondaire –

+0

Ok, mais vous utilisez l'entité WagerBoard dans votre base de données secondaire. –

+0

Si vous utilisez une autre entité du package storingModel dans votre StoreDbEntRepo, cela fonctionnera sans aucune modification, mais pour utiliser l'entité WagerBoard, vous devez l'analyser. –