休眠会话工厂始终为空

时间:2018-07-08 17:33:30

标签: spring hibernate spring-boot spring-data-jpa

我想在Spring应用程序中使用此代码,并通过JNDI使用Hibernate。

@Configuration
@EnableTransactionManagement
public class ContextDatasource {

    @Bean
    public LocalSessionFactoryBean getSessionFactory() throws NamingException {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.plugin.database.models" });
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() throws NamingException {
        return (DataSource) new JndiTemplate().lookup("java:/global/production_gateway");
    }

    @Bean
    public PlatformTransactionManager getHibernateTransactionManager() throws NamingException {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor getExceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        try {
            transactionManager.setSessionFactory(getSessionFactory().getObject());
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return transactionManager;
    }

    private final Properties hibernateProperties() {
        final Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MariaDBDialect");

        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.format_sql", "true");
        return hibernateProperties;
    }
}

当我尝试使用时:

@Autowired
SessionFactory sessionFactory;
..
session = sessionFactory.getCurrentSession();

会话工厂始终为null。我是否缺少一些重要的配置。在程序包部署期间,我没有收到错误消息:

20:15:37,217 INFO  [org.hibernate.Version] (ServerService Thread Pool -- 178) HHH000412: Hibernate Core {5.3.1.Final}
20:15:37,219 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 178) HHH000206: hibernate.properties not found
20:15:37,302 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 178) HCANN000001: Hibernate Commons Annotations {5.0.3.Final}
20:15:37,404 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 178) HHH000400: Using dialect: org.hibernate.dialect.MariaDBDialect
20:15:37,599 INFO  [org.hibernate.orm.beans] (ServerService Thread Pool -- 178) HHH10005002: No explicit CDI BeanManager reference was passed to Hibernate, but CDI is available on the Hibernate ClassLoader.
20:15:38,186 INFO  [stdout] (ServerService Thread Pool -- 178) Hibernate: 

您能给我一些建议吗?

0 个答案:

没有答案