Spring Data JPA

Spring Data JPA

  1. On top of JPA, Spring Data JPA reduces the amount of boilerplate code required by JPA.
  2. Implementation of any interaction with databases, whether relational or NoSQL, using Spring Data is fast and relatively easy.
  3. Spring JPA supports both JPQL and Native Query.

@RepositoryRestResource annotation

https://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/annotation/RepositoryRestResource.html

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:

  1. move the entity classes to the package that contains springboot main class
  2. rename the package containing the main class
  3. 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

https://stackoverflow.com/questions/17842453/is-there-a-workaround-for-ora-01795-maximum-number-of-expressions-in-a-list-is

https://stackoverflow.com/questions/40462110/spring-data-jpa-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:

  1. How to handle null parameters for queries

  2. https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-data-examples/spring-data-jpa-with-h2

  3. https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-data-examples/spring-data-jpa-with-dynamodb

How to implement transactions using Spring JPA

TODO

Spring Data JPA - transactions

Tags

  1. Docker
  2. Spring - Externalized Configuration
  3. Spring Data JPA - working with multiple databases
  4. Spring Data JPA - testing
  5. Spring Data JPA - transactions
  6. Spring Data JPA - Generated Queries
  7. Spring Data JPA - Repositories
  8. Spring Data JPA - Reasons to use
  9. Spring Data JPA - setup and configuration
  10. Spring Data JPA - Batch entity inserts and updates
  11. Spring Data JPA - Single entity inserts and updates
  12. Spring Data JPA - Entities
  13. Spring Data JPA - Queries
  14. Spring Data JPA - Using Projections for Selective Data Retrieval
  15. Spring Data JPA - Pagination
  16. Spring Data JPA - Pagination and Sorting
  17. Spring Data JPA - Dynamic Queries with SpEL Expressions

Reading material

  1. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference
  2. https://www.baeldung.com/persistence-with-spring-series
  3. https://stackoverflow.com/questions/11881479/how-do-i-update-an-entity-using-spring-data-jpa
  4. https://www.baeldung.com/spring-data-partial-update
  5. https://www.javaguides.net/2019/08/spring-boot-crud-rest-api-spring-data-jpa-h2-database-example.html
  6. https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa