Bean 和 SQL 异常 [重复]
Posted
技术标签:
【中文标题】Bean 和 SQL 异常 [重复]【英文标题】:Bean and SQL Exception [duplicate] 【发布时间】:2021-01-14 21:10:14 【问题描述】:我正在使用 Spring Boot 构建一个简单的 CRUD 操作,mysql 和 Hibernate 在此操作中需要帮助。在 Google 上搜索了很多,*** 没有找到合适的解决方案。
存在的问题:服务器时区值“未知”无法识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 serverTimezone
配置属性)以使用更具体的时区值。
com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'unknown' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_91]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_91]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_91]
服务器时区值“未知”无法识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性)以使用更具体的时区值。
Unable to open JDBC Connection for DDL execution
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:69) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.jdbcStatement(GenerationTargetToDatabase.java:77) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:53) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:145) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:316) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
[PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
Error creating bean with name 'digitalEmployeeRepo' defined in com.eg.demo.repository.DigitalEmployeeRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
.................................................. ..................... 我的代码。 ...... 控制器 ....................
package com.eg.demo.controller;
import com.eg.demo.model.DigitalEmployee;
import com.eg.demo.service.DigitalEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
public class DigitalController
@Autowired
DigitalEmployeeService digitalEmployeeService;
@RequestMapping(value = "/DigitalEmployee/", method = RequestMethod.GET)
public ResponseEntity<List<DigitalEmployee>> listAllUsers()
List<DigitalEmployee> digitalEmployees = digitalEmployeeService.allEmployee();
if (digitalEmployees.isEmpty())
return new ResponseEntity(HttpStatus.NO_CONTENT);
return new ResponseEntity<List<DigitalEmployee>>(digitalEmployees, HttpStatus.OK);
// -------------------Retrieve Single User------------------------------------------
@RequestMapping(value = "/digitalEmployee/id", method = RequestMethod.GET)
public ResponseEntity<?> getEmployee(@PathVariable("id") long id)
DigitalEmployee digitalEmployee = digitalEmployeeService.findById(id);
return new ResponseEntity<DigitalEmployee>(digitalEmployee, HttpStatus.OK);
// -------------------Create a User-------------------------------------------
@RequestMapping(value = "/digitalEmployee/", method = RequestMethod.POST)
public ResponseEntity<?> createEmployee(@RequestBody DigitalEmployee digitalEmployee)
digitalEmployeeService.save(digitalEmployee);
return new ResponseEntity<DigitalEmployee>(HttpStatus.CREATED);
// ------------------- Update a User ------------------------------------------------
@RequestMapping(value = "/digitalEmployee/id", method = RequestMethod.PUT)
public ResponseEntity<?> updateEmployee(@PathVariable("id") long id, @RequestBody DigitalEmployee digitalEmployee)
DigitalEmployee currentEmployee = digitalEmployeeService.findById(id);
currentEmployee.setName(digitalEmployee.getName());
digitalEmployeeService.update(currentEmployee);
return new ResponseEntity<DigitalEmployee>(currentEmployee, HttpStatus.OK);
// ------------------- Delete a User-----------------------------------------
@RequestMapping(value = "/digitalEmployee/id", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteEmployee(@PathVariable("id") long id)
DigitalEmployee digitalEmployee = digitalEmployeeService.findById(id);
digitalEmployeeService.deleteById(id);
return new ResponseEntity<DigitalEmployee>(HttpStatus.NO_CONTENT);
// ------------------- Delete All Users-----------------------------
@RequestMapping(value = "/DigitalEmployee/", method = RequestMethod.DELETE)
public ResponseEntity<DigitalEmployee> deleteAll()
digitalEmployeeService.deleteAll();
return new ResponseEntity<DigitalEmployee>(HttpStatus.NO_CONTENT);
型号 ....................
package com.eg.demo.model;
import javax.persistence.*;
@Entity
@Table(name = "DigitalEmployee")
public class DigitalEmployee
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
public DigitalEmployee()
public DigitalEmployee(Long id, String name)
this.id = id;
this.name = name;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
............ 回购 ......
package com.eg.demo.repository;
import com.eg.demo.model.DigitalEmployee;
import org.springframework.data.repository.CrudRepository;
public interface DigitalEmployeeRepo extends CrudRepository<DigitalEmployee, Long>
....................... 服务 …………
package com.eg.demo.service;
import com.eg.demo.model.DigitalEmployee;
import java.util.List;
import java.util.Optional;
public interface DigitalEmployeeService
void save(DigitalEmployee digitalEmployee);
void update(DigitalEmployee digitalEmployee);
void deleteAll();
void deleteById(Long id);
DigitalEmployee findById(Long id);
List<DigitalEmployee> allEmployee();
...... 服务实现 .....
package com.eg.demo.service;
import com.eg.demo.model.DigitalEmployee;
import com.eg.demo.repository.DigitalEmployeeRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DigitalEmployeeServiceImpl implements DigitalEmployeeService
@Autowired
DigitalEmployeeRepo digitalEmployeeRepo;
public void save(DigitalEmployee digitalEmployee)
digitalEmployeeRepo.save(digitalEmployee);
public void update(DigitalEmployee digitalEmployee)
save(digitalEmployee);
public void deleteAll()
digitalEmployeeRepo.deleteAll();
public void deleteById(Long id)
digitalEmployeeRepo.deleteById(id);
public DigitalEmployee findById(Long id)
return digitalEmployeeRepo.findById(id).get();
public List<DigitalEmployee> allEmployee()
return (List<DigitalEmployee>) digitalEmployeeRepo.findAll();
............ 主要方法 ......
package com.eg.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//@Configuration
@SpringBootApplication(scanBasePackages = "com.*")
@EnableSwagger2
//@EnableAutoConfiguration
//@ComponentScan("com.eg.demo")
public class DemoApplication
public static void main(String[] args)
SpringApplication.run(DemoApplication.class, args);
@Bean
public Docket api()
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
....... 应用属性 .....................
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/digital
spring.datasource.username=root
spring.datasource.password=
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true
....................... 绒球 ....................
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eg</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!--<dependency>-->
<!--<groupId>io.springfox</groupId>-->
<!--<artifactId>springfox-swagger2</artifactId>-->
<!--<version>2.9.2</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>io.springfox</groupId>-->
<!--<artifactId>springfox-swagger2</artifactId>-->
<!--<version>2.9.2</version>-->
<!--</dependency>-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-web</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-oas</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>io.springfox</groupId>-->
<!--<artifactId>springfox-swagger-ui</artifactId>-->
<!--<version>2.9.2</version>-->
<!--</dependency>-->
<!---->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
我认为驱动类名应该是com.mysql.cj.jdbc.Driver
【参考方案1】:
使用 datasource url 属性配置时区,就像这样 url:jdbc:mysql://127.0.0.1:3306/test?&serverTimezone=Asia/Shanghai
【讨论】:
我宁愿推荐在这里使用 UTC,否则您最终会遇到难以调试的问题。【参考方案2】:spring.datasource.url=jdbc:mysql://localhost:3306/digital?serverTimezone=UTC
【讨论】:
以上是关于Bean 和 SQL 异常 [重复]的主要内容,如果未能解决你的问题,请参考以下文章