为什么要使用实体经理? [英] Why use an entity manager?
问题描述
我正在使用Hibernate将对象映射到实体,并且我已经开始使用实体管理器。这可能是一个愚蠢的问题,但实际上是使用实体管理器的原因是什么?以前我会使用HibernateSessionFactory来获取会话,然后使用该会话来提取/推送数据。
I am using Hibernate to map objects to entities and I have started to use an Entity Manager. This might be a silly question but what actually is the reason for using the entity manager? Previously I would have used a HibernateSessionFactory to get a session and then use that session to pull/push data.
推荐答案
因为 EntityManager
是标准的一部分 - JPA。从理论上讲,如果需要,您可以切换实现(Hibernate,EclipseLink,OpenJPA)。除了涉嫌可移植性之外,两者之间没有太大的区别。
Because the EntityManager
is part of the standard - JPA. Theoretically, you can switch implementations (Hibernate, EclipseLink, OpenJPA) if you need to. Apart from alleged portability there isn't such a big difference between the two.
Hibernate实现了JPA标准。实际上, EntityManager
具有基于具体实现的委托。对于Hibernate,委托是 Session
。如果你打电话给 getDelegate()
,它将返回当前的会话
。
Hibernate implements the JPA standard. In fact, the EntityManager
has a delegate, based on the concrete implementation. For Hibernate the delegate is the Session
. If you call getDelegate()
it will return the current Session
.
我一直使用带有JPA(EntityManager)的hibernate,并且很少需要获取Session。
I've always used hibernate with JPA (EntityManager) and had very rarely had the need to obtain the Session.
这篇关于为什么要使用实体经理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!