Tuesday, June 18, 2019

Spring Data JPA Interface Methods

Question: What is the difference between Hibernate and Spring Data JPA?
JPA : JPA(java persistence api) is API specifications while Hibernate is the implementation of all the JPA guidelines and Hibernate is a framework.
Spring Data JPA : Hibernate is a JPA implementation, while Spring Data JPA is a JPA data access abstraction. Spring Data JPA cannot work without a JPA provider.
By default Spring Data JPA use Hibernate. please check in spring data dependencies. 
Reference: Spring Data JPA 

Question: How to use Criteria Queries in Spring Boot Data Jpa Application?
JPA 2 introduces a criteria API that you can use to build queries programmatically.
To support specifications, you can extend your repository interface with the JpaSpecificationExecutor interface, as follows:
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
Qusetion: How to call stored procedures in spring Data JPA?
1) we can define stored procedures using @NamedStoredProcedureQueries and name(like test_product), procedureName, parameters(IN/OUT parameter using @StoredProcedureParameter) etc. inside entity class(on top on the class)
2) Then we can call stored procedures inside repository using @Procedure like below.
@Procedure(name = "test_product")
void testProduct(@Param("inParam1") String inParam1);

Question : What is difference between Merge vs Persist in jpa?
In JPA  Persist is used to save new entities like save in hibernate.
In JPA Merge is used to update existing entities like update in hibernate.

Question:  find Vs get spring data jpa? findBy Vs getBy spring data jpa?
Answer: Thre are some difference while any method start with get or find for example.
 User getByEmailAddress(EmailAddress emailAddres)
User findByEmailAddress(EmailAddress emailAdress);
  • get : It will Throws an EmptyResultDataAccessException exception while no record found by the query executed.Throws an IllegalArgumentException when the emailAddress handed to the method is null.
  • find: It will Returns null while no record found by the query executed. Also accepts null as the value for emailAddress. click here to get more details
Question: Difference between getOne and findById in Spring Data JPA?
getOne: This method will always return a proxy without hitting the database (lazily fetched).It internally invokes EntityManager.getReference() method.This method will throw EntityNotFoundException at the time of actual access if the requested entity does not exist in the database. 
 T getOne(ID id);
findById: This method will always hit the database and return the real object mapping to a row in the database. it returns null if no record exists in database.  It is EAGER loaded operation.
Optional<T> findById(ID primaryKey);.

Related Tutorials

No comments:

Post a Comment