'xxxDAOImpl中的字段会话需要一个无法找到'org.hibernate.SessionFactory'类型的bean [英] Field session in 'xxxDAOImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found

查看:166
本文介绍了'xxxDAOImpl中的字段会话需要一个无法找到'org.hibernate.SessionFactory'类型的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误(从我相信的Dao层 - 但我可能会读这个错误)。

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

  * ************************** 
应用程序无法启动
************ ***************

描述:

xx.dao.ParkingSpaceDaoImpl中的字段会话需要一个类型为'org.hibernate .SessionFactory'找不到。


操作:

考虑在你的配置中定义一个类型为org.hibernate.SessionFactory的bean。

在我的DaoImpl文件中,我有:

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

以下是我的POM.xml文件的外观:

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
<包装> jar< / packaging>

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

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

<依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-jdbc< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< /依赖关系>

< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-test< / artifactId>
< scope> test< / scope>
< /依赖关系>

< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.9< / version>
< /依赖关系>


< / dependencies>

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


< / project>

有人有任何想法解决这个问题吗?请让我知道,谢谢。

解决方案

所以我找到了适合我的解决方案。如果您正在使用配置.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 - 处理Hibernate SessionFactory


I am getting the following error (from I believe, the Dao layer - but I could be reading this wrong).

I have a Spring boot app that right now, creates a DB schema. The tables are being create properly but when I tried adding the Dao and DaoImpl files, it crashes with the error message below:

***************************
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.

In my DaoImpl file, I have:

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

Here's how my POM.xml file looks:

<?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>

Does anyone have any idea as to how I fix this? Please let me know, thanks.

解决方案

So I found a solution that works for me. Mathias was probably right if you are working with a configuration .xml file. But for those who are using an application.properties file, you need to add this line to your configuration class or main application class:

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

Once done, add this line to the application.properties file:

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

This solution worked for me. Here are additional references that I was able to work off of:

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

Spring Boot - Handle to Hibernate SessionFactory

这篇关于'xxxDAOImpl中的字段会话需要一个无法找到'org.hibernate.SessionFactory'类型的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆