JPA API与Hibernate原生API的区别及初学者核心技术疑问
Great question—this is a super common point of confusion when you're just starting out with Hibernate and JPA. Let's unpack both of your questions clearly!
First, a quick history lesson: Hibernate existed before JPA was even a thing. It was one of the most popular ORM frameworks, and JPA was actually designed based on ideas from Hibernate (and other ORMs) to create a standard API for persistence in Java.
When JPA was released, Hibernate didn't just wrap itself under JPA—it kept its own native API intact while also implementing the JPA specification. Here's why they sit at the same level in architecture diagrams:
- The JPA API (like
EntityManager,EntityTransaction) is a standardized set of interfaces for basic persistence operations. - Hibernate's native API (like
Session,Transaction) offers extra features that aren't covered by JPA (e.g., specific caching controls, advanced query capabilities, or Hibernate-specific optimizations).
So from an architecture perspective, they're parallel options: you can choose to use JPA's API for portability, Hibernate's native API for more control, or even mix them (since you can convert between EntityManager and Session easily). That's why they're shown as separate but equal layers in the documentation.
EntityManager, and Why Can We Call Its Methods? You're totally right that JPA only provides interfaces—no concrete code. The implementation comes from the JPA provider you're using (in your case, Hibernate).
Here's how it works:
- Hibernate provides the concrete class for
EntityManager: it'sorg.hibernate.jpa.internal.EntityManagerImpl(you don't usually need to interact with this class directly, but it's there under the hood). - You get an instance of this implementation via an
EntityManagerFactory: When you create anEntityManagerFactoryusingPersistence.createEntityManagerFactory("your-persistence-unit-name"), JPA looks at yourpersistence.xml(or your configuration in Spring/Boot) to find which provider to use. Since you're using Hibernate, it loads Hibernate's implementation ofEntityManagerFactory(which isorg.hibernate.jpa.HibernateEntityManagerFactory), and that factory creates instances ofEntityManagerImpl. - Dependency injection simplifies this: If you're using a framework like Spring or Jakarta EE, you can just inject an
EntityManagerwith@PersistenceContext, and the framework handles creating the Hibernate-backed implementation for you.
Let's look at a simple code example to see this in action:
// Create the EntityManagerFactory using Hibernate's implementation EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-pu"); // Get an EntityManager instance (this is actually Hibernate's EntityManagerImpl) EntityManager em = emf.createEntityManager(); // When you call persist(), you're calling Hibernate's implementation em.getTransaction().begin(); em.persist(myEntity); em.getTransaction().commit();
Under the hood, EntityManagerImpl just delegates most operations to Hibernate's native Session—so you're still using Hibernate's core logic, just through the standardized JPA interface.
内容的提问来源于stack exchange,提问作者fbm

