Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. I used LocalContainerEntityManagerFactoryBean to instantiate an instance of EntityManager. Once I obtain an instance of an EntityManager, I can use it to save an entity. For example,

entityManager.merge(someEntity); 

Another option is to use an instance of org.springframework.data.repository.CrudRepository. One, I obtain an instance of a CrudRepository, I can use it to save an entity. For example,

aCrudRepository.save(someEntity); 

What is the difference between using an EntityManager and a CrudRepository to persist an entity into a database ? What are the benefit or disadvantage of the two approach (entity manager vs. crud repository) ?

like image 354
zfranciscus Avatar asked Jan 31 '13 08:01

zfranciscus


People also ask

What is the spring data Repository?

The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.

What is EntityManager in spring?

EntityManager is an interface provided by Java Persistence API (JPA) specification. We use EntityManager as a general-purpose DAO interface for managing lifecycle of entity instances, such as: Create & Remove persistent entity instances. Find entities by their primary key.

What is the difference between JPA Repository and CrudRepository?

Crud Repository is the base interface and it acts as a marker interface. JPA also provides some extra methods related to JPA such as delete records in batch and flushing data directly to a database. It provides only CRUD functions like findOne, saves, etc. JPA repository also extends the PagingAndSorting repository.

What is difference between EntityManagerFactory and SessionFactory?

Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while using SessionFactory will require extra efforts.


1 Answers

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.

like image 163
Jan Bodnar Avatar answered Sep 28 '22 07:09

Jan Bodnar