Java 无法创建请求的服务 [org.hibernate .engine.jdbc.env.spi.JdbcEnvironment]-MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42768265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to create requested service [org.hibernate .engine.jdbc.env.spi.JdbcEnvironment]-MySQL
提问by In-young Choung
I am a newbie to Hibernate. I am currently using Spring boot framework and trying to create database tables through hibernate.
我是 Hibernate 的新手。我目前正在使用 Spring 引导框架并尝试通过休眠创建数据库表。
I know the same question is asked before but I can't seem to figure out how to fix the error based on my environment.
我知道之前有人问过同样的问题,但我似乎无法根据我的环境弄清楚如何修复错误。
hibernate.cfg.xml
休眠文件.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306</property>
<property name="connection_userid">user</property>
<property name="connection_pwd">pass</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection_pool_size">true</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">1</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbmdl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="com.test.springboot.model.AdultParticipant" />
</session-factory>
main class
主类
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");
POJO class
POJO类
@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;
}
}
DAOImpl class
DAOImpl 类
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;
}
}
DAO class
DAO类
public interface AdultParticipantDAO {
public void save(AdultParticipant p);
public List<AdultParticipant> list();
}
POM.xml
POM文件
<?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>
ERROR in Console
控制台中的错误
2017-03-13 11:48:40.512 WARN 9532 --- [ main] org.hibernate.orm.connections.pooling : HHH10001002: Using H
ibernate built-in connection pool (not for production use!)
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate
.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
回答by GauravJ
You need to add mysql
JDBC jar in your dependencies.
您需要mysql
在依赖项中添加JDBC jar。
- Fix your driver class name as
com.mysql.jdbc.Driver
. Fix your username and password property as
"connection.username" for database user "connection.password" for database user password
Create mysql database. See this.
For SSL warning, modify your connection.url to include use ssl false. For example,
jdbc:mysql://localhost:3306/<enter-your-database>?autoReconnect=true&useSSL=false
Modify your mysql dialect to
org.hibernate.dialect.MySQLDialect
- Use
<property name="hbmdl.auto">create-drop</property>
instead of<property name="hbmdl.auto">update</property>
however do notuse this option in production. You should create schema yourself and not do it via hibernate.
- 将您的驱动程序类名称修复为
com.mysql.jdbc.Driver
. 将您的用户名和密码属性修复为
"connection.username" for database user "connection.password" for database user password
创建mysql数据库。看到这个。
对于 SSL 警告,修改您的 connection.url 以包含 use ssl false。例如,
jdbc:mysql://localhost:3306/<enter-your-database>?autoReconnect=true&useSSL=false
将您的 mysql 方言修改为
org.hibernate.dialect.MySQLDialect
- 使用
<property name="hbmdl.auto">create-drop</property>
代替<property name="hbmdl.auto">update</property>
但是没有在生产中使用这个选项。您应该自己创建模式,而不是通过休眠来创建。
回答by UdayKiran Pulipati
Upgrade MySql driver to mysql-connector-java - 8.0.17and
将 MySql 驱动程序升级到mysql-connector-java - 8.0.17和
Those who are using greater than MySQL 5.5version
使用MySQL 5.5 以上版本者
change their driver property
更改他们的驱动程序属性
from com.mysql.jdbc.Driver
to com.mysql.cj.jdbc.Driver
从com.mysql.jdbc.Driver
到com.mysql.cj.jdbc.Driver
because:
因为:
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....
加载类 com.mysql.jdbc.Driver。这已被弃用。新的驱动程序类是 com.mysql.cj.jdbc.Driver。驱动程序通过 SPI 自动注册,通常不需要手动加载驱动程序类。信息 - HHH000401:在 URL 使用驱动程序 [com.mysql.jdbc.Driver]....
in hibernate.properties
在 hibernate.properties
hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver
orif you are using hibernate.cfg.xml
update
或者如果您正在使用hibernate.cfg.xml
更新
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
回答by Beginner
Use HSQLDialect not MYSQLDialect
使用 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
回答by Kapil Thakkar
connection.driver_class
property must be compatible with mysql-connector
jar.
connection.driver_class
属性必须与mysql-connector
jar兼容。
mysql-connector
< 5.5.xx
use com.mysql.jdbc.Driver
mysql-connector
<5.5.xx
使用com.mysql.jdbc.Driver
mysql-connector
> 5.5.xx
use com.mysql.cj.jdbc.Driver
mysql-connector
>5.5.xx
使用com.mysql.cj.jdbc.Driver
回答by jingyuan iu
<property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
You need to set serverTimezone
.
您需要设置 serverTimezone
.
回答by MishraJi
I had the same issue when I deployed my war file to tomcat in my demo environment. My application was not getting started because, in my /tomcat/webapp/ROOT folder, I found my war file Myapp.war there. i.e. In my ROOT folder, it has WEB-INF, META-INF, and Myapp.war, which caused the startup issue. I deleted the war file and it worked.
当我在演示环境中将我的 war 文件部署到 tomcat 时,我遇到了同样的问题。我的应用程序没有启动,因为在我的 /tomcat/webapp/ROOT 文件夹中,我在那里找到了我的战争文件 Myapp.war。即在我的 ROOT 文件夹中,它有 WEB-INF、META-INF 和 Myapp.war,这导致了启动问题。我删除了war文件并且它起作用了。