3

When using spring-data-jpa with hibernate as jpa provider, are the Object/Entity lifecycles same as when using hibernate directly or as defined by hibernate (or might be jpa spec itself).

Hibernate defines these lifecycles to entities - Transient, Persistent, Detached, Removed. Are these same life cycles applicable when using spring-data-jpa too. If so how does below the methods provided by Hibernate map with the methods of spring jpa crud repository.

//below methods in hibernate move an entity to persistent state
save(e), 
persist(e);  
update(e);  
saveOrUpdate(e);  
lock(e);  
merge(e);  

and

//below methods in hibernate move an entity to detached state
detach(e);  
evict(e);  
samshers
  • 1
  • 6
  • 37
  • 84

2 Answers2

5

For the first part of the question:

Spring Data JPA just offers some comfortable mechanics on top of JPA. The persistence, mapping and life cycle is still managed by JPA or its implementation, i.e. Hibernate in your case. This means the life cycle is the same.

As for the mapping between Spring Data JPAs methods and Hibernates/JPA methods see the following table.

Spring Data JPA
CrudRepository.save* for new entities EntityManager.persist, EntityManager.merge otherwise
CrudRepository.delete* EntityManager.remove
CrudRepository.findById EntityManager.find*
JpaRepository.*flush EntityManager.flush
JpaRepository.getById EntityManager.getReference

Other query methods predefined in interfaces or otherwise use various types of queries.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • good mapping of spring data jpa to jpa. +1. if you like the Q can you upvote it so it helps others too. – samshers Sep 11 '21 at 03:29
2

Spring Data Jpa is only an abstraction layer and not provide a lifecycle management. Therefore, if you are using hibernate as a jpa implementation your object's lifecycle will regulated according to hibernate's lifecycle management.

Also, you can find some other explanations here and here as well.

  • great reference +1. thanks for help. if you got time - would you mind helping with this [Criteria API and JPQL API with GROUP BY and CONCAT?](https://stackoverflow.com/questions/73129333/criteria-api-and-jpql-api-with-group-by-and-concat/73163002#73163002) – samshers Jul 30 '22 at 17:06