SpringBoot-多模块项目和数据源-无法扫描@ Entity.class

时间:2019-01-14 10:15:29

标签: spring-boot entity multi-module multiple-databases

我在配置具有多个数据源的多模块项目时遇到了一些麻烦。所以,我需要你的帮助。

我已经准备尝试: Springboot multimodule project SpringBoot ComponentScan issue with multi-module project SpringBoot scanBasePackages not working in multi module project 还有https://mhdevelopment.wordpress.com/2014/09/16/how-to-configure-spring-boot-with-jpa-and-multiple-projects/

但是没有任何作用,我有同样的问题。

因此,这是多模块类路径:

  • BusinessModule,其中包括存储库和实体: 基本软件包: com.mycompany.business
    在该模块中,我有一个外部依赖项,可以在其中找到@Entity类(包: com.mycompany.model
  • RestModule,用于使用BusinessModule。 基本软件包: com.mycompany.rest

业务配置

数据源配置

第一个数据源

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", 
basePackages = {
        "com.mycompany.business.repository", "com.mycompany.model" })
@PropertySource(value = 
"file:${CPROCESS_INSTALL}/server/custom_config/datasources.properties")
public class BusinessDbConfig {

@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "business.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.mycompany.business.model", "com.mycompany.model")
            .persistenceUnit("business").build();
}

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

第二个数据源

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",     transactionManagerRef = "barTransactionManager", basePackages = {
    "com.mycompany.bonita.repository" })
@PropertySource(value =     "file:${CPROCESS_INSTALL}/server/custom_config/datasources.properties")
public class BonitaDbConfig {

@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bonita.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(EntityManagerFactoryBuilder builder,
        @Qualifier("barDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.mycompany.bonita.model")
            .persistenceUnit("bar").build();
}

@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
        @Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
    return new JpaTransactionManager(barEntityManagerFactory);
}

}  

存储库配置

@RepositoryRestResource
public interface CDocumentRepository extends JpaRepository<CDocument, Long> {

@Query("select dp from CDocument d where d.persistenceid = ?1")
public CDocument findByPersistenceId(Long persistenceId);
}  

实体

@javax.persistence.Entity(name = "CDocument")
@Table(name = "CDOCUMENT") 
public class CDocument{ ... }

其余模块配置

应用配置

@SpringBootApplication(scanBasePackages= {"com.mycompany"})
@EnableJpaRepositories("com.mycompany")
@EntityScan("com.mycompany")
@Import({ BonitaDbConfig.class, BusinessDbConfig.class })
public class RestApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(RestApplication.class);
}

public static void main(String[] args) {
    SpringApplication.run(RestApplication.class, args);

}
}

我的控制器

@RestController
public class CreateSpaceController {
private CDocushareConnectRepository cDocushareConnectRepository;
private CDocumentRepository cDocumentRepository;
private DocumentRepository          documentRepository;
private CustomtableRepository       customTableRepository;
private CFieldsMappingRepository    cFieldsMappingRepository;

public CreateSpaceController(CDocumentRepository cDocumentRepository) {
    super();
    this.cDocumentRepository = cDocumentRepository;

}
...
}

当我尝试开始测试或应用程序时,出现此错误:

java.lang.IllegalArgumentException: Not a managed type: class com.mycompany.model.CDocument

我不知道为什么...有什么帮助吗?

0 个答案:

没有答案