获取JPA的Config信息

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.stereotype.Service;

/**
 * @author King
 * @since 6.0.0
 */
@Service
public class SchemeHelper implements InitializingBean, ApplicationContextAware {
    private ApplicationContext applicationContext;
    private LocalContainerEntityManagerFactoryBean entityManagerFactoryBean;

    @Override
    public void afterPropertiesSet() throws Exception {
        entityManagerFactoryBean = (LocalContainerEntityManagerFactoryBean) this.applicationContext.getBean("&entityManagerFactory");
        HibernatePersistenceProvider persistenceProvider = (HibernatePersistenceProvider) entityManagerFactoryBean.getPersistenceProvider();

        persistenceProvider.createContainerEntityManagerFactory(entityManagerFactoryBean.getPersistenceUnitInfo(), entityManagerFactoryBean.getJpaPropertyMap());

        EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(entityManagerFactoryBean.getPersistenceUnitInfo(), entityManagerFactoryBean.getJpaPropertyMap());

        JpaSchemaGenerator.performGeneration(entityManagerFactoryBuilder.getHibernateConfiguration(), entityManagerFactoryBuilder.buildServiceRegistry());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
获取JPA的Config信息
Share this