无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] -MySQL

时间:2017-03-13 16:06:59

标签: java spring hibernate maven

我是Hibernate的新手。我目前正在使用Spring启动框架并尝试通过hibernate创建数据库表。

我知道之前会问过同样的问题,但我似乎无法根据自己的环境弄清楚如何解决错误。

的hibernate.cfg.xml

     public static void main(String[] args) throws Exception {
    SpringApplication.run(WebApplication.class, args);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    AdultParticipant ap = new AdultParticipant();
    ap.setFirstName("User"); 
    ap.setLastName("UserLastName");

    session.persist(ap);

    t.commit();

    session.close();
    System.out.println("successfully saved");

主要班级

@Entity
@Table(name = "adultparticipant")
public class AdultParticipant {

@GeneratedValue
@Id
@Column (name = "id")
private int id;

@Column (name = "firstName")
private String firstName;

@Column (name = "lastName")
private String lastName;


public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}


public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

POJO班

  public class AdultParticipantDAOImpl implements AdultParticipantDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@Override
public void save(AdultParticipant ap) {
    Session session = this.sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.persist(ap);
    tx.commit();
    session.close();
}

@SuppressWarnings("unchecked")
@Override
public List<AdultParticipant> list() {
    Session session = this.sessionFactory.openSession();
    List<AdultParticipant> adultParticipants = session.createQuery("from AdultParticipant").list();
    session.close();
    return adultParticipants;
 }
}

DAOImpl类

public interface AdultParticipantDAO {

public void save(AdultParticipant p);

public List<AdultParticipant> list();

}

DAO课程

 <?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>
<artifactId>hello-springboot</artifactId>
<name>hello-springboot</name>
<description>hello-springboot</description>
<packaging>war</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

的pom.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="my.android.myfirstapp.MainActivity">


<Button
    android:id="@+id/btn_jump"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Jump Activity"
    tools:layout_editor_absoluteX="128dp"
    tools:layout_editor_absoluteY="269dp" />

<EditText
    android:id="@+id/edt_Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text=""
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="62dp" />
</android.support.constraint.ConstraintLayout>

控制台中的错误

2017-03-13 11:48:40.512 WARN 9532 --- [main] org.hibernate.orm.connections.pooling:HHH10001002:使用H ibernate内置连接池(不供生产使用!) 线程&#34; main&#34;中的例外情况org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment]     在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)     at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)     在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)

6 个答案:

答案 0 :(得分:4)

将MySql驱动程序升级到mysql-connector-java - 8.0.17

使用 greater than MySQL 5.5 版本

的用户

更改其驱动程序属性

com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver

因为Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.INFO - HHH000401: using driver [com.mysql.jdbc.Driver] at URL....

hibernate.properties

hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver

(如果您使用的是 hibernate.cfg.xml 更新

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>

答案 1 :(得分:2)

connection.driver_class属性必须与mysql-connector jar兼容。

mysql-connector <5.5.xx使用com.mysql.jdbc.Driver

mysql-connector> 5.5.xx使用com.mysql.cj.jdbc.Driver

答案 2 :(得分:1)

您需要在依赖项中添加mysql JDBC jar。

  1. 将您的驱动程序类名称修改为com.mysql.jdbc.Driver
  2. 将您的用户名和密码属性修改为

    "connection.username" for database user
    "connection.password" for database user password
    
  3. 创建mysql数据库。见this

  4. 对于SSL警告,请修改connection.url以包含使用ssl false。例如,jdbc:mysql://localhost:3306/<enter-your-database>?autoReconnect=true&useSSL=false

  5. 将您的mysql方言修改为org.hibernate.dialect.MySQLDialect

  6. 使用<property name="hbmdl.auto">create-drop</property>代替<property name="hbmdl.auto">update</property>,但在生产中使用此选项。你应该自己创建模式而不是通过hibernate来创建模式。

答案 3 :(得分:1)

使用HSQLDialect而不是MYSQLDialect

spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true


spring.datasource.url=jdbc:mysql://localhost:3306/xyz?serverTimezone=CST6CDT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

答案 4 :(得分:0)

 <property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>

您需要设置serverTimezone

答案 5 :(得分:0)

在我的演示环境中将war文件部署到tomcat时,我遇到了同样的问题。 我的应用程序没有启动,因为在我的/ tomcat / webapp / ROOT文件夹中,我的战争文件Myapp.war在那里。 即在我的ROOT文件夹中 WEB-INF,META-INF和Myapp.war导致了启动问题。 我删除了战争文件,它起作用了。