What is the difference between an Spring Entity Manager and Spring Data Repository?

前端 未结 2 832
离开以前
离开以前 2021-01-30 16:46

I am using JPA in a website. After exploring about options for saving data, I found 2 approach. The first approach is using an implementation of javax.persistence.EntityManager.

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 17:11

    There are several layers of working with persistent data in Java/Spring:

    • JDBC
    • JdbcTemplate
    • JPA (contains EntityManager)
    • Spring Data JPA (contains Repository)

    Each abstraction shields developers from lower-level details, but it can bring its own complexities. JdbcTemplate is a thin abstraction over plain JDBC. Repository is an abstraction over EntityManager. It shields developers from some complex details that are introduced with EntityManager and adds boilerplate code and many convenient methods.

    For instance, CrudRepository adds the implementation of findAll(), which is so common that it makes sense to predefine it. Repositories have many convenience methods for generating queries from method names (convention over configuration), from the entities themselves (Query By Example). They allow to use nice typesafe Fluent API with Query DSL and or enable dynamic projections.

提交回复
热议问题