"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Data JPA - Paging & Sorting Date : 18/04/2025 (Session - 30) _____________________________________________________________________________________________________________________________ Hierarchy ========= Repoistory(I) >>>>> Parent Interface (Marker Interface) ^ | CrudRepoistory(I) >>>>>> Child Interface(12 Abstract Methods, Iterable Objects) ^ | ListCrudRepoistory(CI) >>>>> Child Interface (3 Abstract Methods return List Objects) Repoistory(I) ^ | PagingAndSortingRepoistory(I) >>>>> Child Interface(2 Abstract Methods) |-> Iterable findAll(Sort Object); >>> This method supports for Sorting Entities based on some Property. |-> Page findAll(Pageable pageable) >>> This Method supports for getting Entities based on Pageable Object IIQ) Differences between crudRepoistory & ListCrudRepoistory ? ---> In CrudRepoistory saveAll(), findAll(),findAllById() return types are Iterable object which can't be performed Sorting,Transformation,Filtering etc., ---> In ListCrudRepoistory saveAll(),findAll(),findAllById() return types are List objects which can be performed sorting,Transformation,Filtering etc., * CrudRepoistory(I),PagingAndSortingRepoistory(I) interfaces are common for both SQL Databases & No-SQL Databases. * If We use methods of the above Repoistory interfaces we need not to change code any thing service class even though the application moves from SQL Database Software to No-SQL Database Software. * As part of Spring Data JPA Module we have another important Repoistory interface i.e.,JPARepoistory. * JPARepoistory Interface is a child interface of ListPagingAndSortingRepoistory & ListCrudRepoisotry & QueryByExampleExecutor Interfaces. * JPARepoistory Interface contains 12 abstract Methods * JPARepoistory = CurdRepoistory Interface Methods + PagingAndSortingRepoistory Interface Methods + QueryByExampleExecutor * Most of the methods are available in JPA Repoistory are also available in CrudRepoistory interface and PagingAndSortingRepository Interface but they are work in Underlying JPA Implementation. * For Example in our application DAO Interface extending services from JPARepoistory Interface it will supports the Database Operations for only SQL Database. Differences Between CrudRepoistory and JPARepoistory ==================================================== CrudRepoistory ============== 1) SaveAll(),findAll() Methods return type is Iterable object 2) CrudRepoistory Methods doesn't have support of Example Object. 3) findById() return type is optional Object i.e,Optional and will throw Custom Exception if id is not available 4) CrudRepoistory deleteXXX() will not support Batch Deletion because when we working these methods for each id (or) entity will have seperate delete statement. Example ======= Hibernate: delete from ashokit_customers where customer_id=? Hibernate: delete from ashokit_customers where customer_id=? Hibernate: delete from ashokit_customers where customer_id=? 5) These methods are common for both SQL Databases and NOSQL Databases. JPARepoistory ============= 1) SaveAll(),findAll() methods return type is List Object 2) JPARepoistory Methods having support of Example Object 3) getById() return type is Object i.e., and it will throw EntityNotFoundException supplied id is not available 4) JPARepoistory deleteXXX() will support for Batch Deletion for all entities (or) given ids will be deleted with the help single delete statement. Example ======= Hibernate: delete from ashokit_customers c1_0 where c1_0.customer_id in (?,?,?) 5) These methods are specific to SQL Database only. JpaRepoistory Methods ===================== 1) void deleteAllByIdInBatch(Iterable ids) ========================================== * This method is used to perform bulk deletion (or) batch deletion by taking primary key column values of the Database table. * This Methods won't throws an error If the given id record is not existed in the Database table. * If we remember when we are working deleteAllById(Iterable ids) from CrudRepoistory Interfaces if the given id record is not existed in the database table it will throw an error. * This method can be used only for SQL Database Softwares. 2) List findAll(Example example) ====================================== * This Method is used to return List of object by given Example object based entity object. * Example is an predefined Interfaces from Spring Data JPA Module. * Example Object is JPA Supplied Object containing other object and which can works like Optional class from Java8 Version. Optional opt = Optional.of(customer); >>>> opt.isPresent() >>>>> opt.get() Customer c = new Customer(); c.setCustomerLocation(null); Example example = Example.of(c); customerDao.findAll(example); >>> Retreive the all records for table based on customerName is non null value and simply example object is taking entity object and avoiding null values properties in entity object. * When we pass an entity object to the Example object then entity object allows only non null data will be supplied to Example Object 3) saveAndFlush(entity) =========================== * This method is used to save the given entity into database. * If we are working with CrudRepoistory we used save(entity) method for saving the given entity but where as in JPARepository need to use this method only. * Flushing is the process of synchronizing the state of the persistence context with the underlying database. * When using saveAndFlush() data immediately flush to the database and to do it with the save method we need to call flush() method explicitly. 4) T getReferenceById(Id) ========================= * This Method is used to get the entity object based on given primary key. * When we working with CrudRepoistory for this equivalent method we have Optional findById(ID id) here programmer can have option to throw custom Exception because if given id is not availble in database table. Optional cust = customerDao.findById(123); if(cust.isPresent()){ Customer c = cust.get(); }else{ throw new IllegalArgumentException("Given Id is not existed"); // throw new ResourceNotFoundException("Given ID is not existed"); } * When we are working with this method we do not have option to throw userdefined exception because it already throwing EntityNotFoundException if given id is not existed database.