mardi 14 avril 2015

Explicit use of EntityManager with Spring Data repositories

When do I need to explicitly resort to EntityManager methods(I'm particularly interested in clear(), close(), flush(), detach()) inside my Service methods that additionally work with Spring Data repos?


I'm mostly interested in a general insight into the subject. Let's say in one application I found this:



for ( MyEntity myEntity: entities) {
...some logic here
mySpringDataRepo.save ( myEntity );
}
entityManager.flush ();
entityManager.clear ();


I found such use of EntityManager justifiable since the memory can be overloaded with the entities. However, in another code snippet:



mySpringDataRepository.save(entity);
entityManager.detach(entity);


Is it necessary to explicitly detach the entity? Doesn't Spring Data handle it by itself?


I also found this post: http://ift.tt/1H2nLgy


which confused me even more.


As for the close() method I believe there's no need to call it in the EE environment. Am I correct?


PS: my configurations are very mainstream: JpaTransactionManager/LocalContainerEntityManagerFactoryBean


Aucun commentaire:

Enregistrer un commentaire