I am trying to complete this class which will allow me to switch between repositories. I am not sure how to do it. I want to use default instances via Spring Boot Injection! I know that I am doing it wrong, I cannot pass null value to those class init.
@Configuration
public class ApiRepositoryConfig {
@Bean
@ConditionalOnProperty(name = "db.dialect", havingValue = "postgres", matchIfMissing = true)
public ApiRepository apiJpaRepository() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.tw.api");
return new ApiJpaRepository(ApiApplication.class, emf.createEntityManager());
}
@Bean
@ConditionalOnProperty(name = "db.dialect", havingValue = "mongo")
public ApiRepository apiMongoRepository() {
return new ApiMongoRepository(null, null);
}
}
ApiJpaRepository class
@NoRepositoryBean
public class ApiJpaRepository<T, Id>
extends SimpleJpaRepository<T, Id> implements ApiRepository<T, Id> {
public ApiJpaRepository(Class<T> domainClass, EntityManager entityManager) {
super(domainClass, entityManager);
}
}
ApiMongoRepository class
public class ApiMongoRepository<T, Id> extends SimpleMongoRepository<T, Id> implements ApiRepository<T, Id> {
public ApiMongoRepository(MongoEntityInformation<T, Id> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
}
}
Base Interface Class
public interface ApiRepository<T, Id> extends CrudRepository<T, Id> {
}
Here is the project link if anyone wants to have a full picture, https://github.com/er310/boot-camp/tree/master/api