【问题标题】:Field session in 'xxxDAOImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found“xxxDAOImpl”中的字段会话需要一个“org.hibernate.SessionFactory”类型的 bean,但无法找到
【发布时间】:2017-03-30 02:48:00
【问题描述】:

我收到以下错误(我相信是 Dao 层 - 但我可能读错了)。

我现在有一个 Spring Boot 应用程序,它创建了一个数据库模式。表正在正确创建,但是当我尝试添加 Dao 和 DaoImpl 文件时,它会崩溃并显示以下错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field session in xx.dao.ParkingSpaceDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.


Action:

Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.

在我的 DaoImpl 文件中,我有:

@Repository 
public class xxDaoImpl implements xxDao {
    @Autowired
    private SessionFactory session;

这是我的 POM.xml 文件的外观:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xx.xx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> 
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

有人知道我该如何解决这个问题吗?请让我知道,谢谢。

【问题讨论】:

    标签: hibernate spring-mvc spring-data


    【解决方案1】:

    所以我找到了一个适合我的解决方案。如果您使用的是配置 .xml 文件,Mathias 可能是正确的。但是对于那些使用 application.properties 文件的人,您需要将这一行添加到您的配置类或主应用程序类中:

    @Bean  
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){  
        return hemf.getSessionFactory();  
    }   
    

    完成后,将此行添加到 application.properties 文件:

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

    这个解决方案对我有用。以下是我能够处理的其他参考资料:

    http://www.ekiras.com/2016/02/how-to-use-configure-session-factory-bean-springboot.html

    Spring Boot - Handle to Hibernate SessionFactory

    【讨论】:

    • 有没有办法不止一次给你点赞?我一直在寻找 xml free configuration 有一段时间了,这是唯一有效的。
    • 此外,就我而言,我必须在使用SessionFactory 的方法上使用@Transactional 注释。 :) 谢谢。
    • HibernateEntityManagerFactory 是从哪里来的?
    【解决方案2】:

    首先确保 Hibernate 依赖项位于您的类路径中。

    如果是,你应该在你的 Spring applicationContext 中定义你的 SessionFactory。

    如果您使用的是 Hibernate4,配置必须是这样的:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    
        <property name="packagesToScan">
            <list>
                <value>com.gya.model</value>
            </list>
        </property>
    </bean>
    

    此定义允许您连接到您的数据库,如果找不到有效的 SessionFactory,Spring 将引发异常。

    【讨论】:

    • 如果我不使用hibernate.cfg.xml文件而只使用注解怎么办?我对 Spring/Hibernate 比较陌生,我正在使用 Spring STS IDE 创建一个入门项目。
    猜你喜欢
    • 2017-09-17
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2019-02-13
    • 2023-02-15
    相关资源
    最近更新 更多