Search code examples
springjpaspring-dataentitymanager

Is it better to use JpaRepository or EntityManager in a Spring application?


I have used both approaches, but what's the difference and which is better?


Solution

  • If your query is simple and basic enough such that it can be achieved by Spring data 's query generation feature , use Repository over entity manager will save you some times and effort.

    If your query cannot be easily achieved by the query generation feature or you need to fine tune them , use entity manager which gives you the most of the flexibility.

    For CRUD only, both of them are more or less the same as entity manager API itself is already clean and simple enough to use.