创建实体管理器工厂时的服务exeption

时间:2018-03-30 16:33:27

标签: java mysql

我今天尝试构建我的项目并且在连接到MySQL时遇到问题。我收到一个异常:org.hibernate.service.spi.ServiceException:当我尝试创建实体管理器工厂时,无法创建请求的服务[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;
}

}

1 个答案:

答案 0 :(得分:0)

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