https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html
JPA 中很多方法的反射调用已经利用了 MethodHandle 做优化
Spring Boot 自动装配
pkg org.springframework.boot.autoconfigure.orm.jpa
pkg org.springframework.boot.autoconfigure.data.jpa
由 LocalContainerEntityManagerFactoryBean 装配产生一个符合 JPA 规范的 javax.persistence.EntityManagerFactory
// 类信息
package org.springframework.orm.jpa;
public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean
implements ResourceLoaderAware, LoadTimeWeaverAware
public abstract class AbstractEntityManagerFactoryBean implements
FactoryBean<EntityManagerFactory>, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware,
InitializingBean, DisposableBean, EntityManagerFactoryInfo, PersistenceExceptionTranslator, Serializable
// 流程
LocalContainerEntityManagerFactoryBean#afterPropertiesSet
- 加载一个 Spring PersistenceUnitManager 提供 javax.persistence.spi.PersistenceUnitInfo, Manager 主要(Supports standard JPA scanning for {@code persistence.xml} files,
* with configurable file locations, JDBC DataSource lookup and load-time weaving.)(即包含了项目配置的 Model, Converter, JDBC, 切面增强等)
- 加载 org.springframework.orm.jpa.JpaVendorAdapter, 提供 JPA 实现的具体配置 ()
AbstractEntityManagerFactoryBean#afterPropertiesSet (super)
- 从 JpaVendorAdapter 获取 javax.persistence.spi.PersistenceProvider (SpringHibernateJpaPersistenceProvider)
- 从 JpaVendorAdapter 获取 Class<? extends EntityManagerFactory> entityManagerFactoryInterface
- 从 JpaVendorAdapter 获取 Class<? extends EntityManager> entityManagerInterface
- 从 JpaVendorAdapter 获取 JpaDialect (#releaseJdbcConnection, #getJdbcConnection, #cleanupTransaction, #prepareTransaction, #beginTransaction)
- 为 this 设置 PersistenceProvider, jpaPropertyMap, Class entityManagerFactoryInterface, Class entityManagerInterface,
JpaDialect
- this.nativeEntityManagerFactory = createNativeEntityManagerFactory, 使用 PersistenceProvider#createContainerEntityManagerFactor(PersistenceUnitInfo,Map jpaProperties)
创建 EntityManagerFactory(由 class org.hibernate.internal.SessionFactoryImpl)
- this.entityManagerFactory = createEntityManagerFactoryProxy(this.nativeEntityManagerFactory);
代理 LocalContainerEntityManagerFactoryBean, 生成具有事务 ManagedEntityManagerFactoryInvocationHandler,
每个调用均会被代理至 AbstractEntityManagerFactoryBean#invokeProxyMethod
AbstractJpaVendorAdapter
(created by JpaBaseConfiguration, HibernateJpaConfiguration)
// 三属性 spring.jpa.showSql, spring.jpa.database.*, spring.jpa.generateDdl
this.persistenceProvider = new SpringHibernateJpaPersistenceProvider();
this.entityManagerFactoryInterface = org.hibernate.jpa.HibernateEntityManagerFactory.class;
this.entityManagerInterface = org.hibernate.jpa.HibernateEntityManager.class;
adapter.setShowSql(this.properties.isShowSql());
adapter.setDatabase(this.properties.getDatabase());
adapter.setDatabasePlatform(this.properties.getDatabasePlatform());
adapter.setGenerateDdl(this.properties.isGenerateDdl());
Spring 会调用由 org.springframework.orm.jpa.SharedEntityManagerCreator 生成 SpringContext 管理的 EntityManager 代理
A shared EntityManager will behave just like an EntityManager fetched from
* an application server's JNDI environment, as defined by the JPA specification.
* It will delegate all calls to the current transactional EntityManager, if any;
* otherwise it will fall back to a newly created EntityManager per operation.
或者由 ExtendedEntityManagerCreator 生成外部管理的 EntityManager 代理