Tuesday, June 18, 2019

Spring Data JPA Interface Methods

Q- 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.

Q- 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
Q- 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. 
Example:

 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.
Example:

Optional<T> findById(ID primaryKey);.







Related Tutorials


No comments:

Post a Comment