如何在 Spring Boot 中自动装配 Hibernate SessionFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43895643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:24:50  来源:igfitidea点击:

How to autowire Hibernate SessionFactory in Spring boot

springhibernatespring-boot

提问by Renaud is Not Bill Gates

I've created a spring boot application, and I want to handle the Hibernate SessionFactory, so in my service class, I can just call the Hibernate SessionFactory as following :

我创建了一个 Spring Boot 应用程序,我想处理 Hibernate SessionFactory,所以在我的服务类中,我可以按如下方式调用 Hibernate SessionFactory:

@Autowired
private SessionFactory sessionFactory;

I found a similar question in stackoverflow where I have to add the following line in application.properties :

我在 stackoverflow 中发现了一个类似的问题,我必须在 application.properties 中添加以下行:

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

but I'm getting this error :

但我收到此错误:

Cannot resolve property 'current_session_context_class' in java.lang.String

How can I solve this ?

我该如何解决这个问题?

pom.xml dependencies :

pom.xml 依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

采纳答案by BartoszMiller

Try enabling HibernateJpaSessionFactoryBeanin your Spring configuration.

尝试HibernateJpaSessionFactoryBean在您的 Spring 配置中启用。

@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
    return new HibernateJpaSessionFactoryBean();
}

Have a look at: https://stackoverflow.com/a/33881946/676731

看看:https: //stackoverflow.com/a/33881946/676731

By Spring configuration I mean a class annotated with @Configurationannotation or @SpringBootApplication(it is implicitly annotated with @Configuration).

通过 Spring 配置,我的意思是用@Configurationannotation 或@SpringBootApplication(它隐式地用 注释@Configuration)注释的类。

回答by Karim M. Fadel

Since version 2.0, JPA provides easy access to the APIs of the underlying implementations. The EntityManagerand the EntityManagerFactoryprovide an unwrapmethod which returns the corresponding classes of the JPA implementation.

从 2.0 版开始,JPA 提供了对底层实现 API 的轻松访问。的EntityManagerEntityManagerFactory提供unwrap返回JPA实现的相应的类的方法。

In the case of Hibernate, these are the Sessionand the SessionFactory.

在 Hibernate 的情况下,这些是SessionSessionFactory

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);