What's the Difference Between Spring Data's Mongotemplate and Mongorepository

What's the difference between Spring Data's MongoTemplate and MongoRepository?

"Convenient" and "powerful to use" are contradicting goals to some degree. Repositories are by far more convenient than templates but the latter of course give you more fine-grained control over what to execute.

As the repository programming model is available for multiple Spring Data modules, you'll find more in-depth documentation for it in the general section of the Spring Data MongoDB reference docs.

TL;DR

We generally recommend the following approach:

  1. Start with the repository abstract and just declare simple queries using the query derivation mechanism or manually defined queries.
  2. For more complex queries, add manually implemented methods to the repository (as documented here). For the implementation use MongoTemplate.

Details

For your example this would look something like this:

  1. Define an interface for your custom code:

    interface CustomUserRepository {

    List<User> yourCustomMethod();
    }
  2. Add an implementation for this class and follow the naming convention to make sure we can find the class.

    class UserRepositoryImpl implements CustomUserRepository {

    private final MongoOperations operations;

    @Autowired
    public UserRepositoryImpl(MongoOperations operations) {

    Assert.notNull(operations, "MongoOperations must not be null!");
    this.operations = operations;
    }

    public List<User> yourCustomMethod() {
    // custom implementation here
    }
    }
  3. Now let your base repository interface extend the custom one and the infrastructure will automatically use your custom implementation:

    interface UserRepository extends CrudRepository<User, Long>, CustomUserRepository {

    }

This way you essentially get the choice: everything that just easy to declare goes into UserRepository, everything that's better implemented manually goes into CustomUserRepository. The customization options are documented here.

Can we use both MongoRepository and MongoTemplate in same application

Though MongoRepository is much simpler to use than MongoTemplate, but template gives you more fine grained control over the queries that you're implementing.

Having said that you can still be implementing complex queries on using MongoRepository like this:

@Query("{'id': ?#{ [0] ? {$exists :true} : [1] }}")
List<Person> findByQueryWithExpressionAndNestedObject(boolean param0, String param1);

More information on can be found here

But as a practise it's actually good to have both in your code, that is

  1. Use Repository when you want to use that is already implemented.
  2. Manually implement few custom methods using the mongo-template when you want to implement customMethod which has complex logics.

Implementation

Step 1:

interface CustomEmployeeRepository{
List<Employee> CustomEmployeeMethod(String field)
}

Step 2 :

@Repository
public class CustomEmployeeRepositoryImpl implements CustomEmployeeRepository{

@Autowired
private MongoTemplate mongotemplate;

@Override
List<Employee> CustomEmployeeMethod(String field){

}
}

Step 3:
Now create a new repository extending MongoRepository and CustomEmployeeRepository to use the custom implementations as well as predefined methods of repository.

interface EmployeeRepository extends MongoRepository<Employee, long>, CustomEmployeeRepository{
}

This way you get the best of both world. Refer to the documentation for more information on the implementation.
Hope this helps.

What is the difference between JpaRepository and MongoRepository?

JPARepository and MongoRepository are technology-specific abstraction of the Spring Data Repositories.

If you are using RDBMS such as MySQL/PostgreSQL, you may use Spring Data Repositories such as JpaRepository.
If using a NoSQL such as Mongo, you will need the MongoReposiroty.

spring data mongodb , use MongoRepository or MongoTemplate?

ok, by looking at source code MongoRepository consume mongoTemplate and provide a set of common DAO API so in other words, use MongoRepository is preferred way.

What's the difference between Spring Data MongoDB and Hibernate OGM for MongoDB?

Disclaimer: I am the lead of the Spring Data project, so I'll mostly cover the Spring Data side of things here:

I think the core distinction between the two projects is that the Hibernate OGM team chose to center their efforts around the JPA while the Spring Data team explicitly did not. The reasons are as follows:

  • JPA is an inherently relational API. The first two sentences of the spec state, that it's an API for object-relational mapping. This is also embodied in core themes of the API: it talks about tables, columns, joins, transactions. Concepts that are not necessarily transferable into the NoSQL world.
  • You usually choose a NoSQL store because of its special traits (e.g. geospatial queries on MongoDB, being able to execute graph traversals for Neo4j). None of them are (and will be) available in JPA, hence you'll need to provide proprietary extensions anyway.
  • Even worse, JPA features concepts that will simply guide users into wrong directions if they assume them to work on a NoSQL store like they were defined in JPA: how should a transaction rollback be implemented reasonably on top of a MongoDB?

So with Spring Data, we chose to rather provide a consistent programming model for the supported stores but not try to force everything into a single over-abstracting API: you get the well-known template implementations, you get the repository abstraction, which works identical for all stores but lets you leverage store-specific features and concepts.



Related Topics



Leave a reply



Submit