创建名为“entityManagerFactory”的bean时出错:[PersistenceUnit:默认]无法构建Hibernate SessionFactory

Posted

技术标签:

【中文标题】创建名为“entityManagerFactory”的bean时出错:[PersistenceUnit:默认]无法构建Hibernate SessionFactory【英文标题】:Error creating bean with name 'entityManagerFactory' : [PersistenceUnit: default] Unable to build Hibernate SessionFactory 【发布时间】:2019-12-07 14:47:45 【问题描述】:

我在运行无法构建休眠会话工厂的项目时遇到错误

控制台出错

Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/8.5.49
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Nov 17 2019 18:45:30 UTC
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 8.5.49.0
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_66
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_66-b17
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\dolly.kumbhalkar\Documents\Dolly\Projects\AnySpaces\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 8.5
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\dolly.kumbhalkar\Documents\Dolly\Projects\AnySpaces\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.5
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\dolly.kumbhalkar\Documents\Dolly\Projects\AnySpaces\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.5\endorsed
Dec 07, 2019 2:13:05 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Dec 07, 2019 2:13:05 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_66\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_66/bin/server;C:/Program Files/Java/jre1.8.0_66/bin;C:/Program Files/Java/jre1.8.0_66/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Users\dolly.kumbhalkar\AppData\Local\Programs\Git\cmd;C:\Users\dolly.kumbhalkar\Desktop;;.]
Dec 07, 2019 2:13:05 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8081"]
Dec 07, 2019 2:13:06 PM org.apache.tomcat.util.net.NioselectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Dec 07, 2019 2:13:06 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8010"]
Dec 07, 2019 2:13:06 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Dec 07, 2019 2:13:06 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2313 ms
Dec 07, 2019 2:13:06 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Dec 07, 2019 2:13:06 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.49
Dec 07, 2019 2:13:07 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [108] milliseconds.
Dec 07, 2019 2:13:29 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Dec 07, 2019 2:13:30 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 07, 2019 2:13:30 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
2019-12-07 14:13:30.178 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization started 
2019-12-07 14:13:30.849 [localhost-startStop-1] INFO  o.s.w.c.s.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sat Dec 07 14:13:30 IST 2019]; root of context hierarchy 
2019-12-07 14:13:31.152 [localhost-startStop-1] INFO  o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml] 
2019-12-07 14:13:33.662 [localhost-startStop-1] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' 
2019-12-07 14:13:34.132 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 3935 ms 
Dec 07, 2019 2:13:34 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'anyspace-dispatcher'
2019-12-07 14:13:34.171 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'anyspace-dispatcher': initialization started 
2019-12-07 14:13:34.191 [localhost-startStop-1] INFO  o.s.w.c.s.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'anyspace-dispatcher-servlet': startup date [Sat Dec 07 14:13:34 IST 2019]; parent: Root WebApplicationContext 
2019-12-07 14:13:34.193 [localhost-startStop-1] INFO  o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/anyspace-dispatcher-servlet.xml] 
2019-12-07 14:13:40.408 [localhost-startStop-1] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'persistenceJPAConfig' of type [class in.techchefs.anyspace.common.data.configuration.PersistenceJPAConfig$$EnhancerBySpringCGLIB$$fe458096] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
2019-12-07 14:13:40.814 [localhost-startStop-1] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$21496ae6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
2019-12-07 14:13:41.122 [localhost-startStop-1] INFO  o.s.j.d.DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver 
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
2019-12-07 14:13:41.898 [localhost-startStop-1] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' 
2019-12-07 14:13:47.888 [localhost-startStop-1] WARN  o.s.w.c.s.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in in.techchefs.anyspace.common.data.configuration.PersistenceJPAConfig: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
2019-12-07 14:13:47.895 [localhost-startStop-1] ERROR o.s.web.servlet.DispatcherServlet - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in in.techchefs.anyspace.common.data.configuration.PersistenceJPAConfig: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:985)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4885)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5199)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:884)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    ... 28 common frames omitted
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to obtain JDBC Connection
    at org.hibernate.tool.schema.internal.exec.AbstractJdbcConnectionContextImpl.getConnection(AbstractJdbcConnectionContextImpl.java:46)
    at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:59)
    at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcDatabaseMetaData(ImprovedExtractionContextImpl.java:66)
    at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.getTable(InformationExtractorJdbcDatabaseMetaDataImpl.java:271)
    at org.hibernate.tool.schema.internal.exec.ImprovedDatabaseInformationImpl.getTableInformation(ImprovedDatabaseInformationImpl.java:109)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.performMigration(SchemaMigratorImpl.java:252)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:137)
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:110)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:176)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:64)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:458)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881)
    ... 34 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)

这是我的 JPA 配置类:PersistenceJPAConfig.java

package in.common.data.configuration;

import java.util.Properties;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@PropertySource("classpath:application.$env.properties")
@EnableTransactionManagement
public class PersistenceJPAConfig 

    public static final String DB_MYSQL_DRIVER_PROPERTY = "database.mysql.driver-class";
    public static final String DB_MYSQL_HOST_PROPERTY = "database.mysql.host";
    public static final String DB_MYSQL_PORT_PROPERTY = "database.mysql.port";
    public static final String DB_MYSQL_DATABASE_PROPERTY = "database.mysql.database";
    public static final String DB_MYSQL_USERNAME_PROPERTY = "database.mysql.username";
    public static final String DB_MYSQL_PASSWORD_PROPERTY = "database.mysql.password";
    public static final String HIBERNATE_HBM2DDL_PROPERTY = "hibernate.hbm2ddl.auto";
    public static final String HIBERNATE_DIALECT_PROPERTY = "hibernate.dialect";
    public static final String HIBERNATE_SHOW_SQL_PROPERTY = "hibernate.show_sql";

    @Autowired
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("in.techchefs.anyspace");
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    

    @Bean
    public DataSource dataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty(DB_MYSQL_DRIVER_PROPERTY));
        dataSource.setUrl("jdbc:mysql://"+env.getProperty(DB_MYSQL_HOST_PROPERTY)+":"+env.getProperty(DB_MYSQL_PORT_PROPERTY)+"/"+env.getProperty(DB_MYSQL_DATABASE_PROPERTY));
        dataSource.setUsername(env.getProperty(DB_MYSQL_USERNAME_PROPERTY));
        dataSource.setPassword(env.getProperty(DB_MYSQL_PASSWORD_PROPERTY));

        return dataSource;
    

    @Bean
    public PlatformTransactionManager transactionManager() 
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

        return transactionManager;
    

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() 
        return new PersistenceExceptionTranslationPostProcessor();
    

    Properties additionalProperties() 
        Properties properties = new Properties();
        properties.setProperty(HIBERNATE_HBM2DDL_PROPERTY, env.getProperty(HIBERNATE_HBM2DDL_PROPERTY));
        properties.setProperty(HIBERNATE_DIALECT_PROPERTY, env.getProperty(HIBERNATE_DIALECT_PROPERTY));
        properties.setProperty(HIBERNATE_SHOW_SQL_PROPERTY, env.getProperty(HIBERNATE_SHOW_SQL_PROPERTY));

        return properties;
    

application.properties

database.mysql.driver-class=com.mysql.jdbc.Driver
database.mysql.host=localhost
database.mysql.port=3306
database.mysql.database=anyspace_dev_1504
database.mysql.username=root
database.mysql.password=root@123
hibernate.hbm2ddl.auto=update
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.ejb.use_class_enhancer=true

这里是 pom.xml 文件,我使用的是 8.0.18 的 mysql 服务器,是否有任何原因会出现此错误,因为我在依赖项 5.1.28 中提到过,但如果我更改它,导入 com.mysql.jdbc 会出现另一个错误.StringUtils;

pom.xml

<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>in.techchefs.anyspace</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>common</name>
    <description>Common for AnySpace</description>

    <parent>
        <groupId>in.techchefs.anyspace</groupId>
        <artifactId>anyspace</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <dependencies>

        <!-- Spring Framework dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>$spring.version</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>$logback.version</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>$servlet.api.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-websocket</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <!-- JPA -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.11.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <!-- For connection pooling -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>$hibernate.version</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>$hibernate.version</version>
        </dependency>
        <!-- Database -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jfree/jfreechart -->
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jfree/jcommon -->
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.16</version>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>xero-java-mvn-repo</id>
            <url>https://raw.github.com/XeroAPI/Xero-Java/mvn-repo/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

    <!-- <repositories> <repository> <id>codelds</id> <url>https://code.lds.org/nexus/content/groups/main-repo</url> 
        </repository> </repositories> -->
    <build>
        <finalName>common</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>$maven-eclipse-plugin.version</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>$wtpversion</wtpversion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>$maven-compiler-plugin.version</version>
                <configuration>
                    <source>$jdk.version</source>
                    <target>$jdk.target.version</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

请使用依赖版本号而不是配置的属性名称更新您的 pom.xml。 【参考方案1】:

我认为堆栈跟踪可以很好地引导您找到根本原因。

你有一个异常告诉你Error creating bean with name 'entityManagerFactory'

按照堆栈跟踪,您可以看到它是Caused by: Unable to build Hibernate SessionFactory

Caused by: Unable to obtain JDBC Connection

Caused by: Could not create connection to database server.

因此,此时您的问题实际上并不是您无法创建 entityManagerFactory bean,而是您的应用程序无法连接到您的数据库。因此,您只需在application.properties 文件中检查与数据库的连接。我希望这能回答你的问题。

【讨论】:

以上是关于创建名为“entityManagerFactory”的bean时出错:[PersistenceUnit:默认]无法构建Hibernate SessionFactory的主要内容,如果未能解决你的问题,请参考以下文章

创建名为“entityManagerFactory”的bean时出错:[PersistenceUnit:默认]无法构建Hibernate SessionFactory

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“entityManagerFactory”的bean时出错

使用 JPA 进行 Spring 启动 - 创建名为“entityManagerFactory”的 bean 时出错 - driverClassName 需要 jdbcUrl

Spring Boot 项目“org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的be

SpringBoot JPA Hibernate:创建名为'entityManagerFactory'的bean时出错

NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用