My code has been working fine with just a single repository package, until I began adding more repository & entity packages in my data source config class. The code failed with the exception :... Error creating bean with name 'waecChemistryObjRepo' defined in com.example.resource.akademiks.waec.obj.dao.WaecChemistryObjRepo defined in @EnableJpaRepositories declared on AkademiksDbConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.resource.akademiks.waec.obj.entity.WaecChemistryObj
Primary data source config class:
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.example.resource.akademiks.waec.obj.dao",
"com.example.resource.akademiks.waec.theory.dao", "com.example.resource.akademiks.waec.theory.answer.dao"},
entityManagerFactoryRef = "akademiksEntityManagerFactory",
transactionManagerRef = "akademiksTransactionManager")
public class AkademiksDbConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource.waecakademiks")
public DataSourceProperties akademiksDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean
@ConfigurationProperties("spring.datasource.waecakademiks")
public DataSource akademiksDataSource() {
return akademiksDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "akademiksEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
akademiksEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(akademiksDataSource())
.packages("com.example.resource.akademiks.waec.obj.entity")
.packages("com.example.resource.akademiks.waec.theory.questions.entity")
.packages("com.example.resource.akademiks.waec.theory.answer.entity")
.build();
}
@Primary
@Bean
public PlatformTransactionManager akademiksTransactionManager(
@Qualifier("akademiksEntityManagerFactory")
LocalContainerEntityManagerFactoryBean akademiksEntityManagerFactory
) {
return new JpaTransactionManager(akademiksEntityManagerFactory.getObject());
}
}
Repository:
package com.example.resource.akademiks.waec.obj.dao;
@Repository
@RepositoryRestResource(collectionResourceRel = "chemistry", path = "chemistry")
public interface WaecChemistryObjRepo extends JpaRepository<WaecChemistryObj, Integer> {
service layers:
package com.example.resource.akademiks.waec.theory.chemistry.service;
public interface WaecChemistryTheoryService {}
Service implementation:
@Service
public class WaecChemistryObjServiceImpl implements WaecChemistryObjService {
@Autowired
private WaecChemistryObjOptionRepo chemistryOptionRepo;
entity class:
package com.example.resource.akademiks.waec.obj.entity;
@Entity
@Table(name = "WaecChemistryObjAnswer")
public class WaecChemistryObjAnswer {
Other entities and repositories have been excluded.
CodePudding user response:
The packages
method takes a varargs parameter:
EntityManagerFactoryBuilder.Builder packages(String... packagesToScan)
You call it 3 times instead of passing 3 packages in a single call.
More generally: take advantage of auto-configuration and follow convention over configuration approach, as suggested in comments.