Spring Entity Manager和Spring Data Repository有什么区别? [英] What is the difference between an Spring Entity Manager and Spring Data Repository?
问题描述
我正在网站上使用JPA.在研究了用于保存数据的选项之后,我发现了两种方法.第一种方法是使用javax.persistence.EntityManager的实现.我使用LocalContainerEntityManagerFactoryBean实例化EntityManager的实例.一旦获得EntityManager的实例,就可以使用它来保存实体.例如,
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);
另一个选择是使用org.springframework.data.repository.CrudRepository的实例.一,我获得一个CrudRepository的实例,我可以用它来保存一个实体.例如,
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);
使用EntityManager和CrudRepository将实体持久存储到数据库之间有什么区别?两种方法(实体管理器与Crud存储库)的优点或缺点是什么?
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) ?
推荐答案
这两个接口提供了两种不同的方法:
This two interfaces presents two different approaches:
-
org.springframework.data.repository.CrudRepository
是提供CRUD操作的存储库的通用接口. -
javax.persistence.EntityManager
与持久性相关联 语境. Spring具有自己的接口,该接口扩展了CrudRepository
称为
org.springframework.data.repository.CrudRepository
is common interface for repositories which provide CRUD operations.javax.persistence.EntityManager
is associated with a persistence context. Spring has its own interface which extendsCrudRepository
calledJpaRepository
for this purposes.
I hope you know about benefits or disadvantages of persistent API. If you don't you can read answers to this question.
这篇关于Spring Entity Manager和Spring Data Repository有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!