📜  JPA与Hibernate

📅  最后修改于: 2021-01-02 15:58:20             🧑  作者: Mango

JPA与Hibernate

什么是JPA?

JPA(Java持久性API)是Java的规范,用于访问,管理和持久化Java对象与关系数据库之间的数据。它被视为对象关系映射的标准方法。

JPA可以看作是面向对象的域模型和关系数据库系统之间的桥梁。作为规范,JPA本身不会执行任何操作。因此,它需要实施。因此,Hibernate,TopLink和iBatis等ORM工具实现了JPA规范以实现数据持久性。

什么是休眠?

Hibernate是一个Java框架,用于将Java对象存储在关系数据库系统中。它是一个开源的,轻量级的ORM(对象关系映射)工具。

Hibernate是JPA的实现。因此,它遵循JPA提供的通用标准。

需要JPA

到目前为止我们已经看到,JPA是一个规范。它为ORM工具提供了通用的原型和功能。通过实现相同的规范,所有ORM工具(如Hibernate,TopLink,iBatis)都遵循通用标准。将来,如果要将我们的应用程序从一种ORM工具切换到另一种,则可以轻松完成。

JPA与Hibernate

JPA Hibernate
Java Persistence API (JPA) defines the management of relational data in the Java applications. Hibernate is an Object-Relational Mapping (ORM) tool which is used to save the state of Java object into the database.
It is just a specification. Various ORM tools implement it for data persistence. It is one of the most frequently used JPA implementation.
It is defined in javax.persistence package. It is defined in org.hibernate package.
The EntityManagerFactory interface is used to interact with the entity manager factory for the persistence unit. Thus, it provides an entity manager. It uses SessionFactory interface to create Session instances.
It uses EntityManager interface to create, read, and delete operations for instances of mapped entity classes. This interface interacts with the persistence context. It uses Session interface to create, read, and delete operations for instances of mapped entity classes. It behaves as a runtime interface between a Java application and Hibernate.
It uses Java Persistence Query Language (JPQL) as an object-oriented query language to perform database operations. It uses Hibernate Query Language (HQL) as an object-oriented query language to perform database operations.