Spring Data JPA
Spring Data JPA
- On top of JPA, Spring Data JPA reduces the amount of boilerplate code required by JPA.
- Implementation of any interaction with databases, whether relational or NoSQL, using Spring Data is fast and relatively easy.
- Spring JPA supports both JPQL and Native Query.
@RepositoryRestResource annotation
Spring Data REST takes the features of Spring HATEOAS and Spring Data MongoDB and automatically combines them together.
@RepositoryRestResource is not required for a repository to be exported. It is used only to change the export details, such as using /people instead of the default value of /persons.
You don’t have to write a RestController.
Sample implementation: https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-data-examples/spring-data-rest-with-mongodb
gotchas while working with spring-boot-starter-data-jpa
Not a managed type error
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carService' defined in file [/home/h/Downloads/GitRepositories/programming-playground/java-playground/spring-data-jpa-using-h2/target/classes/com/mycompany/springdatajpausingh2/inventory/services/CarService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'carRepository' defined in com.mycompany.springdatajpausingh2.inventory.repositories.CarRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.mycompany.springdatajpausingh2.inventory.entities.Car
why: spring could not find entity classes
cause: Entity classes are not under the package that contains springboot main class
solution:
- move the entity classes to the package that contains springboot main class
- rename the package containing the main class
- use @EntityScan annotation
If all of them fail, make sure you are using import jakarta.persistence.Entity;
as opposed to import javax.persistence.Entity;
in the entities.
ORA-01795: maximum number of expressions in a list is 1000
List<TransactRepViewModel> result = null;
final List<List<String>> partitions = ListUtils.partition(clientIdList, 999);
for (List<String> partition : partitions) {
if (ObjectUtils.isEmpty(result)) {
result = new ArrayList<TransactRepViewModel>();
}
result.addAll(yourRepo.findByClientIdList(partition, startDate, endDate);)
}
How to implement locking using Spring JPA
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#locking
Implementations
This shows a few things:
-
How to handle null parameters for queries
How to implement transactions using Spring JPA
TODO
Spring Data JPA - transactions
Tags
- Docker
- Spring - Externalized Configuration
- Spring Data JPA - working with multiple databases
- Spring Data JPA - testing
- Spring Data JPA - transactions
- Spring Data JPA - Generated Queries
- Spring Data JPA - Repositories
- Spring Data JPA - Reasons to use
- Spring Data JPA - setup and configuration
- Spring Data JPA - Batch entity inserts and updates
- Spring Data JPA - Single entity inserts and updates
- Spring Data JPA - Entities
- Spring Data JPA - Queries
- Spring Data JPA - Using Projections for Selective Data Retrieval
- Spring Data JPA - Pagination
- Spring Data JPA - Pagination and Sorting
- Spring Data JPA - Dynamic Queries with SpEL Expressions
Reading material
- https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference
- https://www.baeldung.com/persistence-with-spring-series
- https://stackoverflow.com/questions/11881479/how-do-i-update-an-entity-using-spring-data-jpa
- https://www.baeldung.com/spring-data-partial-update
- https://www.javaguides.net/2019/08/spring-boot-crud-rest-api-spring-data-jpa-h2-database-example.html
- https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa