【问题标题】:service exeption when creating entity manager factory创建实体管理器工厂时服务异常
【发布时间】:2018-09-09 16:54:56
【问题描述】:

我今天尝试构建我的项目,但在连接到 MySQL 时遇到问题。我收到一个异常: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 当我尝试创建我的实体管理器工厂时。昨天工作正常。我检查了 MySql 是否正在运行并尝试重新启动计算机,但没有任何效果。

这是我的 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="VictoryPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.stevie.memberfx.model.HouseholdEntity</class>
    <class>com.stevie.memberfx.model.MemberEntity</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/victorydb?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.user" value="stephen"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="ozzie2014"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
      <property name="hibernate.show_sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

和我创建实体管理器工厂的代码:

public class JpaUtil {

private static final EntityManagerFactory entityManagerFactory;

static {
    try {
        entityManagerFactory = Persistence.createEntityManagerFactory("VictoryPU");
    } catch (Throwable ex) {
        // Log the exception. 
        System.out.println("Initial EntityManagerFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static EntityManagerFactory getEntityManagerFactory() {
    return entityManagerFactory;
}

}

【问题讨论】:

    标签: java mysql


    【解决方案1】:

    您必须将 mysql 驱动程序更改为版本 5,并将您的 persistence.xml 文件中的驱动程序名称更改为旧名称 com.mysql.jdbc.Driver

    【讨论】:

    • 成功了!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2014-05-20
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多