欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Error creating bean with name 'entityManagerFactory' defined in class path resource解决方案

程序员文章站 2023-08-31 09:00:21
​ 项目是集成了Spring Boot和Spring Data,然后简单的把Spring Data Jpa和Spring Boot配置完成,开始进行公司项目的重构,然后出现了这个问题。当时也是找了挺多的资料,后来发现时javaBean的问题。 在Spring Data Jpa和实体类进行映射的时候, ......

​ 项目是集成了spring boot和spring data,然后简单的把spring data jpa和spring boot配置完成,开始进行公司项目的重构,然后出现了这个问题。当时也是找了挺多的资料,后来发现时javabean的问题。

在spring data jpa和实体类进行映射的时候,要特别注意实体类型和set、get方法名是否对应。

error starting applicationcontext. to display the conditions report re-run your application with 'debug' enabled.
2019-11-24 21:25:31.117 error 30608 --- [  restartedmain] o.s.boot.springapplication               : application run failed

org.springframework.beans.factory.beancreationexception: error creating bean with name 'entitymanagerfactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaconfiguration.class]: invocation of init method failed; nested exception is javax.persistence.persistenceexception: [persistenceunit: default] unable to build hibernate sessionfactory; nested exception is org.hibernate.mappingexception: could not get constructor for org.hibernate.persister.entity.singletableentitypersister
    at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1803) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:595) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:517) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractbeanfactory.lambda$dogetbean$0(abstractbeanfactory.java:323) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:321) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:202) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:1108) ~[spring-context-5.2.1.release.jar:5.2.1.release]
    at org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:868) ~[spring-context-5.2.1.release.jar:5.2.1.release]
    at org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:550) ~[spring-context-5.2.1.release.jar:5.2.1.release]
    at org.springframework.boot.springapplication.refresh(springapplication.java:747) ~[spring-boot-2.2.1.release.jar:2.2.1.release]
    at org.springframework.boot.springapplication.refreshcontext(springapplication.java:397) ~[spring-boot-2.2.1.release.jar:2.2.1.release]
    at org.springframework.boot.springapplication.run(springapplication.java:315) ~[spring-boot-2.2.1.release.jar:2.2.1.release]
    at org.springframework.boot.springapplication.run(springapplication.java:1226) ~[spring-boot-2.2.1.release.jar:2.2.1.release]
    at org.springframework.boot.springapplication.run(springapplication.java:1215) ~[spring-boot-2.2.1.release.jar:2.2.1.release]
    at com.silverbullet.ydserver2.ydserver2.ydserver2application.main(ydserver2application.java:20) ~[classes/:na]
    at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:na]
    at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.method.invoke(method.java:566) ~[na:na]
    at org.springframework.boot.devtools.restart.restartlauncher.run(restartlauncher.java:49) ~[spring-boot-devtools-2.2.1.release.jar:2.2.1.release]
caused by: javax.persistence.persistenceexception: [persistenceunit: default] unable to build hibernate sessionfactory; nested exception is org.hibernate.mappingexception: could not get constructor for org.hibernate.persister.entity.singletableentitypersister
    at org.springframework.orm.jpa.abstractentitymanagerfactorybean.buildnativeentitymanagerfactory(abstractentitymanagerfactorybean.java:403) ~[spring-orm-5.2.1.release.jar:5.2.1.release]
    at org.springframework.orm.jpa.abstractentitymanagerfactorybean.afterpropertiesset(abstractentitymanagerfactorybean.java:378) ~[spring-orm-5.2.1.release.jar:5.2.1.release]
    at org.springframework.orm.jpa.localcontainerentitymanagerfactorybean.afterpropertiesset(localcontainerentitymanagerfactorybean.java:341) ~[spring-orm-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1862) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1799) ~[spring-beans-5.2.1.release.jar:5.2.1.release]
    ... 20 common frames omitted
caused by: org.hibernate.mappingexception: could not get constructor for org.hibernate.persister.entity.singletableentitypersister
    at org.hibernate.persister.internal.persisterfactoryimpl.createentitypersister(persisterfactoryimpl.java:123) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.persister.internal.persisterfactoryimpl.createentitypersister(persisterfactoryimpl.java:77) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.metamodel.internal.metamodelimpl.initialize(metamodelimpl.java:181) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.internal.sessionfactoryimpl.<init>(sessionfactoryimpl.java:305) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.boot.internal.sessionfactorybuilderimpl.build(sessionfactorybuilderimpl.java:462) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl.build(entitymanagerfactorybuilderimpl.java:1249) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.springframework.orm.jpa.vendor.springhibernatejpapersistenceprovider.createcontainerentitymanagerfactory(springhibernatejpapersistenceprovider.java:58) ~[spring-orm-5.2.1.release.jar:5.2.1.release]
    at org.springframework.orm.jpa.localcontainerentitymanagerfactorybean.createnativeentitymanagerfactory(localcontainerentitymanagerfactorybean.java:365) ~[spring-orm-5.2.1.release.jar:5.2.1.release]
    at org.springframework.orm.jpa.abstractentitymanagerfactorybean.buildnativeentitymanagerfactory(abstractentitymanagerfactorybean.java:391) ~[spring-orm-5.2.1.release.jar:5.2.1.release]
    ... 24 common frames omitted
caused by: org.hibernate.propertyaccessexception: exception occurred inside getter of com.silverbullet.ydserver2.ydserver2.model.packsortinglog.id
    at org.hibernate.property.access.spi.gettermethodimpl.get(gettermethodimpl.java:45) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.engine.internal.unsavedvaluefactory.getunsavedidentifiervalue(unsavedvaluefactory.java:68) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.tuple.propertyfactory.buildidentifierattribute(propertyfactory.java:62) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.tuple.entity.entitymetamodel.<init>(entitymetamodel.java:136) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.persister.entity.abstractentitypersister.<init>(abstractentitypersister.java:571) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at org.hibernate.persister.entity.singletableentitypersister.<init>(singletableentitypersister.java:124) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    at java.base/jdk.internal.reflect.nativeconstructoraccessorimpl.newinstance0(native method) ~[na:na]
    at java.base/jdk.internal.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.constructor.newinstance(constructor.java:490) ~[na:na]
    at org.hibernate.persister.internal.persisterfactoryimpl.createentitypersister(persisterfactoryimpl.java:96) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    ... 32 common frames omitted
caused by: java.lang.reflect.invocationtargetexception: null
    at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:na]
    at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.method.invoke(method.java:566) ~[na:na]
    at org.hibernate.property.access.spi.gettermethodimpl.get(gettermethodimpl.java:42) ~[hibernate-core-5.4.8.final.jar:5.4.8.final]
    ... 42 common frames omitted
caused by: java.lang.nullpointerexception: null
    at com.silverbullet.ydserver2.ydserver2.model.packsortinglog.getid(packsortinglog.java:26) ~[classes/:na]
    ... 47 common frames omitted


process finished with exit code 0

Error creating bean with name 'entityManagerFactory' defined in class path resource解决方案

字段id设置的是包装类型long,但是我在get上面注入返回的是基本类型long,所以注入失败。

将基本数据类型long改成long就可以。

注:也有的是可能get和set方法名有问题。可以检查一下。

文章为arvinhan原创,如果文章有错的地方欢迎指正,大家互相交流。