- ORM - is a technique used to map Java objects to database tables. All the operation that we
do with java object will be performed in database.
- It allows developer to work with databases using OOP concept, making it easier to interact with relational databases.
- ORM frameworks - like hibernate.
- How does it work ?
- Consider a Java class User and a database table users.
- ORM framework like Hibernate can map the fields in the User class to columns in the users table, making it easier to insert, update, retrieve and delete the record.
- JPA is a way to achieve ORM, it includes interfaces and annotations that you use in Java classes, requires a persistence provider(ORM TOOL) for implementation.
- To use JPA, you need a persistence provider. Ex of JPA persistence providers includes - Hibernate, EclipseLink and OpenJPA. These providers implement the JPA interface and provider
the underlying functionality to interact with databases.
Spring JPA --> JPA --> Hibernate(implementation of JPA) --> Database - JPA is primarily designed for working with relational databases, where data is stored in table with predefined schema.
- MongoDb, on other hand is NoSQL database, which are schema less/flexible schema.
- Hence JPA is not used with MongoDB.
- Here
Spring Data MongoDBserves as the persistence provider for MongoDB, It provides the necessary abstraction and implementations to work with MongoDB in a Spring application.
- Methods to interact with databases :
- Query Based: simple by writing the method names.
- Criteria API: Helps with making complex queries.
- Spring Data JPA is build on top of JPA, but it is not the implementation of JPA.
- The purpose of Spring JPA is to make working with JPA easy.
Controller --> service --> Repository