1

I am very new to spring and mongo db.Please suggest the way for my problem: I have this web app written in Spring boot and it mainly uses JPA for database. I have a multiple entity and then I have multiple repositories that extends JPARepository with some methods that fetch data from the database.

But now I need to have a second database and it must be MongoDB. Everything is identical to the JPA, but now I have a new repository class, that this time extends MongoRepository.

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

public interface CustomerRepositoryMongo extends MongoRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

So now I have two classes, CustomerRepository and CustomerRepositoryMongo. 1.First of all what all changes i have to made to run it in a mongodb inside the eclipse Ide and how can i configure mongodb inside eclipse IDE.? 2.How can i easily switch between two dbs,how to specify which repository to used? 3.I have no idea how can i achieve this also i can not have both the interfaces with the same name. 4.I have tried the jpa approach with @profile annotation,its working fine..but to use the mongodb ,i have to create the collection for all the entities which has @table ,@entity annotation.So shall i use @document annotation as well to specify the collection name because i am using the different names in my queries.

Anuj
  • 327
  • 1
  • 2
  • 14
  • Have you gone through their official documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-two-datasources – Abhilekh Singh Oct 25 '17 at 06:03
  • yes ,i have gone through that but i am not getting what exactly i need to do – Anuj Oct 25 '17 at 06:45
  • Have you try this solution here? https://stackoverflow.com/questions/37502329/switch-between-jpa-and-mongo-in-spring-boot-mvc-app – amandasantanati Mar 02 '18 at 00:46

0 Answers0