简体   繁体   English

Session 和 EntityManager 是一样的吗?

[英]Are Session and EntityManager the same?

New to JPA and was wondering if Session (hibernate) and EntityManager are the same thing or not? JPA 的新手,想知道 Session(休眠)和 EntityManager 是否相同? As both of them basically allow you to manage and search for entities in the relational database.因为它们基本上都允许您管理和搜索关系数据库中的实体。

Also any good source materials on the 2 would be helpful.此外,关于 2 的任何好的源材料都会有所帮助。 Thank you in advance!先感谢您!

Hibernate first appeared in 2001. Among the object-relational mapping frameworks that emerged in Java, it was probably the most famous, complete, and successful. Hibernate 最早出现于 2001 年。在 Java 中出现的对象-关系映射框架中,它可能是最著名、最完整、最成功的。 It uses Session.它使用 Session。

However, Hibernate is a specific framework developed by a specific group.但是,Hibernate 是特定组开发的特定框架。 In 2005, several groups came together under the coordination of Sun to develop and establish an official standard to define in generic terms the behavior of object-relational mapping frameworks. 2005 年,几个小组在 Sun 的协调下共同开发和建立了一个官方标准,以通用术语定义对象-关系映射框架的行为。 Furthermore, it was defined that this framework would use the new features of the Java 5 language (released in 2004), since no mature object-relational framework used them (generic types and annotations, mainly).此外,定义该框架将使用 Java 5 语言(2004 年发布)的新特性,因为没有成熟的对象关系框架使用它们(主要是泛型类型和注释)。 The works were heavily inspired by Hibernate and there was a lot of participation from the Hibernate people.这些作品深受 Hibernate 的启发,并有很多 Hibernate 人的参与。 From this, JPA was born in 2006. JPA uses EntityManager.由此,JPA诞生于2006年。JPA使用EntityManager。

Another goal to be achieved with the creation of the JPA was the elimination of BMP and CMP from EJB 2 (will not be missed).创建 JPA 要实现的另一个目标是从 EJB 2 中消除 BMP 和 CMP(不会错过)。 Basically CMP was an attempt to create a persistence specification and object-relational mapping, but it was quite complicated, difficult to use, tightly coupled to EJB 2 and offered far fewer features than Hibernate.基本上 CMP 是一种创建持久性规范和对象关系映射的尝试,但它非常复杂,难以使用,与 EJB 2 紧密耦合,并且提供的功能远少于 Hibernate。 BMP was something closer to what entity beans (annotated with @Entity) are used by JPA today, although they were in a much less developed state. BMP 更接近于今天 JPA 使用的实体 bean(用 @Entity 注释),尽管它们在 state 中的开发要少得多。 But that is already a subject for another topic.但这已经是另一个主题的主题了。

When JPA was finally born, Hibernate obviously started to implement JPA, thereby gaining adherence to the official standard and also the use of generic types and annotations.当 JPA 终于诞生时,Hibernate 显然开始实现 JPA,从而获得了对官方标准的遵守以及泛型类型和注释的使用。 That is, he came to recognize everything that the JPA recognizes.也就是说,他开始识别 JPA 识别的一切。 In early versions of Hibernate with JPA support, the EntityManager redirected everything it did to the Session.在支持 JPA 的 Hibernate 的早期版本中,EntityManager 将它所做的一切重定向到 Session。 In newer versions of Hibernate (5.2+), this is even simpler because the Session interface extends the EntityManager interface directly.在较新版本的 Hibernate (5.2+) 中,这更加简单,因为 Session 接口直接扩展了 EntityManager 接口。

However, Hibernate is just one of the existing implementations of JPA (although it is a very good one and is the most successful one).然而,Hibernate 只是 JPA 的现有实现之一(虽然它是一个非常好的实现,也是最成功的实现)。 Other implementations of JPA have emerged JPA的其他实现已经出现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM