Spring引导找不到类型为“ org.hibernate.SessionFactory”的bean

【字号: 日期:2024-03-23浏览:16作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决Spring引导找不到类型为“ org.hibernate.SessionFactory”的bean?

我的问题是我期望Spring Boot JPA自动配置能够为Hibernate DAO自动配置并连接SessionFactory。

SessionFactory特定于Hibernate,而Spring Boot会自动关联EntityManager对象,这对JPA更通用。

在我的Dao对象中将SessionFactory交换为EntityManager允许我使用Spring的自动配置来连接一切。

解决方法

我似乎无法弄清楚为什么我的SpringBoot应用程序无法自动创建SessionFactory。据我了解,它应该自动拾取属性文件并能够创建会话工厂bean。

我需要在某些Dao类中与Hibernate一起使用会话工厂,但是下面的简单示例重现了我遇到的问题:

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>mytools</groupId><artifactId>spring-boot-hello-world</artifactId><version>1.0-SNAPSHOT</version><properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <start-class>mytools.spring.hello.Application</start-class></properties><dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.0.1.RELEASE</version> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>2.0.1.RELEASE</version> </dependency> <dependency><groupId>org.hsqldb</groupId><artifactId>hsqldb</artifactId><version>2.3.4</version> </dependency> <dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version> </dependency></dependencies><build> <plugins><plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> <executions><execution> <goals><goal>repackage</goal> </goals></execution> </executions></plugin> </plugins></build>

application.properties:

spring.datasource.url=jdbc:hsqldb:file:databaseFiles/hibData/;hsqldb.write_delay_millis=0spring.datasource.root=saspring.datasource.password=1spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContextspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialectspring.jpa.properties.hibernate.show_sql=falsespring.jpa.properties.hibernate.hbm2ddl.auto=updatespring.jpa.properties.hibernate.jdbc.batch_size=50spring.jpa.properties.hibernate.order_inserts=truespring.jpa.properties.hibernate.order_updates=truespring.jpa.properties.hibernate.jdbc.batch_versioned_data=true

应用类别:

package mytools.spring.hello;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { @Autowired SessionFactory sessionFactory; public static void main(String[] args) {SpringApplication.run(Application.class,args); }}

输出:

'C:Program FilesJavajdk-9.0.4binjava' -Dvisualvm.id=13106393222439 '-javaagent:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2017.1.4libidea_rt.jar=51221:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2017.1.4bin' -Dfile.encoding=UTF-8 -classpath D:ProjectsJavaspringboothelloworldtargetclasses;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-web2.0.1.RELEASEspring-boot-starter-web-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter2.0.1.RELEASEspring-boot-starter-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot2.0.1.RELEASEspring-boot-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-autoconfigure2.0.1.RELEASEspring-boot-autoconfigure-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-logging2.0.1.RELEASEspring-boot-starter-logging-2.0.1.RELEASE.jar;C:Usersowner.m2repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;C:Usersowner.m2repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;C:Usersowner.m2repositoryorgapachelogginglog4jlog4j-to-slf4j2.10.0log4j-to-slf4j-2.10.0.jar;C:Usersowner.m2repositoryorgapachelogginglog4jlog4j-api2.10.0log4j-api-2.10.0.jar;C:Usersowner.m2repositoryorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;C:Usersowner.m2repositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;C:Usersowner.m2repositoryorgspringframeworkspring-core5.0.5.RELEASEspring-core-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-jcl5.0.5.RELEASEspring-jcl-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgyamlsnakeyaml1.19snakeyaml-1.19.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-json2.0.1.RELEASEspring-boot-starter-json-2.0.1.RELEASE.jar;C:Usersowner.m2repositorycomfasterxmljacksoncorejackson-databind2.9.5jackson-databind-2.9.5.jar;C:Usersowner.m2repositorycomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;C:Usersowner.m2repositorycomfasterxmljacksoncorejackson-core2.9.5jackson-core-2.9.5.jar;C:Usersowner.m2repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.9.5jackson-datatype-jdk8-2.9.5.jar;C:Usersowner.m2repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.9.5jackson-datatype-jsr310-2.9.5.jar;C:Usersowner.m2repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.9.5jackson-module-parameter-names-2.9.5.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-tomcat2.0.1.RELEASEspring-boot-starter-tomcat-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgapachetomcatembedtomcat-embed-core8.5.29tomcat-embed-core-8.5.29.jar;C:Usersowner.m2repositoryorgapachetomcatembedtomcat-embed-el8.5.29tomcat-embed-el-8.5.29.jar;C:Usersowner.m2repositoryorgapachetomcatembedtomcat-embed-websocket8.5.29tomcat-embed-websocket-8.5.29.jar;C:Usersowner.m2repositoryorghibernatevalidatorhibernate-validator6.0.9.Finalhibernate-validator-6.0.9.Final.jar;C:Usersowner.m2repositoryjavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;C:Usersowner.m2repositoryorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;C:Usersowner.m2repositorycomfasterxmlclassmate1.3.4classmate-1.3.4.jar;C:Usersowner.m2repositoryorgspringframeworkspring-web5.0.5.RELEASEspring-web-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-beans5.0.5.RELEASEspring-beans-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-webmvc5.0.5.RELEASEspring-webmvc-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-aop5.0.5.RELEASEspring-aop-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-context5.0.5.RELEASEspring-context-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-expression5.0.5.RELEASEspring-expression-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-data-jpa2.0.1.RELEASEspring-boot-starter-data-jpa-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-aop2.0.1.RELEASEspring-boot-starter-aop-2.0.1.RELEASE.jar;C:Usersowner.m2repositoryorgaspectjaspectjweaver1.8.13aspectjweaver-1.8.13.jar;C:Usersowner.m2repositoryorgspringframeworkbootspring-boot-starter-jdbc2.0.1.RELEASEspring-boot-starter-jdbc-2.0.1.RELEASE.jar;C:Usersowner.m2repositorycomzaxxerHikariCP2.7.8HikariCP-2.7.8.jar;C:Usersowner.m2repositoryorgspringframeworkspring-jdbc5.0.5.RELEASEspring-jdbc-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorghibernatehibernate-core5.2.16.Finalhibernate-core-5.2.16.Final.jar;C:Usersowner.m2repositoryorghibernatejavaxpersistencehibernate-jpa-2.1-api1.0.0.Finalhibernate-jpa-2.1-api-1.0.0.Final.jar;C:Usersowner.m2repositoryorgjavassistjavassist3.22.0-GAjavassist-3.22.0-GA.jar;C:Usersowner.m2repositoryantlrantlr2.7.7antlr-2.7.7.jar;C:Usersowner.m2repositoryorgjbossjandex2.0.3.Finaljandex-2.0.3.Final.jar;C:Usersowner.m2repositorydom4jdom4j1.6.1dom4j-1.6.1.jar;C:Usersowner.m2repositoryorghibernatecommonhibernate-commons-annotations5.0.1.Finalhibernate-commons-annotations-5.0.1.Final.jar;C:Usersowner.m2repositoryjavaxtransactionjavax.transaction-api1.2javax.transaction-api-1.2.jar;C:Usersowner.m2repositoryorgspringframeworkdataspring-data-jpa2.0.6.RELEASEspring-data-jpa-2.0.6.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkdataspring-data-commons2.0.6.RELEASEspring-data-commons-2.0.6.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-orm5.0.5.RELEASEspring-orm-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgspringframeworkspring-tx5.0.5.RELEASEspring-tx-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;C:Usersowner.m2repositoryorgspringframeworkspring-aspects5.0.5.RELEASEspring-aspects-5.0.5.RELEASE.jar;C:Usersowner.m2repositoryorghsqldbhsqldb2.3.4hsqldb-2.3.4.jar;C:Usersowner.m2repositoryjavaxxmlbindjaxb-api2.3.0jaxb-api-2.3.0.jar mytools.spring.hello.Application . ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___,| / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::(v2.0.1.RELEASE)2018-05-12 00:46:57.170 INFO 7944 --- [ main] mytools.spring.hello.Application : Starting Application on Hulk with PID 7944 (D:ProjectsJavaspringboothelloworldtargetclasses started by owner in D:ProjectsJavaspringboothelloworld)2018-05-12 00:46:57.170 INFO 7944 --- [ main] mytools.spring.hello.Application : No active profile set,falling back to default profiles: default2018-05-12 00:46:57.220 INFO 7944 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5340477f: startup date [Sat May 12 00:46:57 EEST 2018]; root of context hierarchyWARNING: An illegal reflective access operation has occurredWARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/owner/.m2/repository/org/springframework/spring-core/5.0.5.RELEASE/spring-core-5.0.5.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,java.security.ProtectionDomain)WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operationsWARNING: All illegal access operations will be denied in a future release2018-05-12 00:46:58.078 INFO 7944 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean ’org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration’ of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$6f5d8399] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2018-05-12 00:46:58.370 INFO 7944 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2018-05-12 00:46:58.390 INFO 7944 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2018-05-12 00:46:58.390 INFO 7944 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.292018-05-12 00:46:58.395 INFO 7944 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:Program FilesJavajdk-9.0.4bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.0bin;C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.0libnvvp;C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.1bin;C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.1libnvvp;C:IntelOpenCLsdkbinx64;C:IntelOpenCLsdkbinx86;C:IntelOpenCLsdkbinPin;C:IntelOpenCLsdkbinGTPin;C:Program FilesHaskell Platform8.2.1libextralibsbin;C:Program FilesHaskell Platform8.2.1bin;C:Program FilesNVIDIA GPU Computing ToolkitCUDAv8.0bin;C:Program FilesNVIDIA GPU Computing ToolkitCUDAv8.0libnvvp;C:ProgramDataOracleJavajavapath;C:Program FilesMicrosoft MPIBin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program FilesGitcmd;C:Program FilesMicrosoft SQL Server130ToolsBinn;C:Program Filesdotnet;C:Program FilesAnaconda3;C:Program FilesAnaconda3Scripts;C:Program FilesAnaconda3Librarybin;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program FilesHaskell Platform8.2.1mingwbin;D:RunnableBinsmyrepos;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)HaskellStackbin;C:UsersownerAppDataRoamingcabalbin;C:UsersownerAppDataLocalMicrosoftWindowsApps;;.]2018-05-12 00:46:58.480 INFO 7944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2018-05-12 00:46:58.480 INFO 7944 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1260 ms2018-05-12 00:46:58.570 INFO 7944 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]2018-05-12 00:46:58.575 INFO 7944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ’characterEncodingFilter’ to: [/*]2018-05-12 00:46:58.575 INFO 7944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ’hiddenHttpMethodFilter’ to: [/*]2018-05-12 00:46:58.575 INFO 7944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ’httpPutFormContentFilter’ to: [/*]2018-05-12 00:46:58.575 INFO 7944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ’requestContextFilter’ to: [/*]2018-05-12 00:46:58.695 INFO 7944 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...2018-05-12 00:46:58.875 INFO 7944 --- [ main] hsqldb.db..ENGINE: open start - state modified2018-05-12 00:46:58.885 INFO 7944 --- [ main] hsqldb.db..ENGINE: checkpointClose start2018-05-12 00:46:58.885 INFO 7944 --- [ main] hsqldb.db..ENGINE: checkpointClose synched2018-05-12 00:46:58.895 INFO 7944 --- [ main] hsqldb.db..ENGINE: checkpointClose script done2018-05-12 00:46:58.915 INFO 7944 --- [ main] hsqldb.db..ENGINE: checkpointClose end2018-05-12 00:46:58.915 INFO 7944 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported)2018-05-12 00:46:58.915 INFO 7944 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.2018-05-12 00:46:58.945 INFO 7944 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit ’default’2018-05-12 00:46:58.960 INFO 7944 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...]2018-05-12 00:46:59.010 INFO 7944 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.16.Final}2018-05-12 00:46:59.010 INFO 7944 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found2018-05-12 00:46:59.040 INFO 7944 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}2018-05-12 00:46:59.120 INFO 7944 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect2018-05-12 00:46:59.320 INFO 7944 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit ’default’2018-05-12 00:46:59.335 WARN 7944 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ’application’: Unsatisfied dependency expressed through field ’sessionFactory’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ’org.hibernate.SessionFactory’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}2018-05-12 00:46:59.335 INFO 7944 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit ’default’2018-05-12 00:46:59.335 INFO 7944 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...2018-05-12 00:46:59.380 INFO 7944 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.2018-05-12 00:46:59.385 INFO 7944 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]2018-05-12 00:46:59.390 INFO 7944 --- [ main] ConditionEvaluationReportLoggingListener :Error starting ApplicationContext. To display the conditions report re-run your application with ’debug’ enabled.2018-05-12 00:46:59.450 ERROR 7944 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :***************************APPLICATION FAILED TO START***************************Description:Field sessionFactory in mytools.spring.hello.Application 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.Process finished with exit code 1

相关文章: