在闲暇之余使用Hibernate做个练习顺便回顾一下曾经的使用经历,没成想,在开发过程中遇到了大大小小的问题,但是其中一个问题对于我来说比较郁闷,就是标题所描述的这个问题,此问题找了半天,也没找到啥原因,上个刚搭建时的配置文件:
com.mysql.cj.jdbc.Driver
jdbc:mysql://localhost:3306/hibernatetest?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
root
123
这个是刚开始配置的文件,但是呢,这个配置在编辑器里边是报错的,如图:
报错的地方描述如下:
错误信息使用有道翻译过来,是:
看着翻译的内容得出xml中不支持这种方式的连接符, 然后将其中的&符号替换成了分号“;”,然后错误就消失了
改为分号时,错误消失了,这时就可以尝试继续测试程序了,测试程序如下:
package com.hibernateTest;
import java.util.Date;
import com.hibernate.domain.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTest {
@Test
public void test() {
Customer c = new Customer("张三", false, new Date());
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(c);
transaction.commit();
session.close();
sessionFactory.close();
}
}
但是,天公不做美,控制台有抛出了异常信息,内容如下:
D:\software\tools\installDir\jdk8\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\software\tools\installDir\idea\IntelliJ IDEA Community Edition 2017.1.6\lib\idea_rt.jar=54951:D:\software\tools\installDir\idea\IntelliJ IDEA Community Edition 2017.1.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\software\tools\installDir\idea\IntelliJ IDEA Community Edition 2017.1.6\lib\idea_rt.jar;D:\software\tools\installDir\idea\IntelliJ IDEA Community Edition 2017.1.6\plugins\junit\lib\junit-rt.jar;D:\software\tools\installDir\jdk8\jre\lib\charsets.jar;D:\software\tools\installDir\jdk8\jre\lib\deploy.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\access-bridge-64.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\cldrdata.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\dnsns.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\jaccess.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\jfxrt.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\localedata.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\nashorn.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\sunec.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\sunjce_provider.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\sunmscapi.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\sunpkcs11.jar;D:\software\tools\installDir\jdk8\jre\lib\ext\zipfs.jar;D:\software\tools\installDir\jdk8\jre\lib\javaws.jar;D:\software\tools\installDir\jdk8\jre\lib\jce.jar;D:\software\tools\installDir\jdk8\jre\lib\jfr.jar;D:\software\tools\installDir\jdk8\jre\lib\jfxswt.jar;D:\software\tools\installDir\jdk8\jre\lib\jsse.jar;D:\software\tools\installDir\jdk8\jre\lib\management-agent.jar;D:\software\tools\installDir\jdk8\jre\lib\plugin.jar;D:\software\tools\installDir\jdk8\jre\lib\resources.jar;D:\software\tools\installDir\jdk8\jre\lib\rt.jar;E:\workspace\idea_workspace\ormtest\target\classes;F:\repository\lib\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar;F:\repository\lib\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;F:\repository\lib\org\hibernate\hibernate-core\5.0.12.Final\hibernate-core-5.0.12.Final.jar;F:\repository\lib\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.jar;F:\repository\lib\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;F:\repository\lib\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;F:\repository\lib\antlr\antlr\2.7.7\antlr-2.7.7.jar;F:\repository\lib\org\apache\geronimo\specs\geronimo-jta_1.1_spec\1.1.1\geronimo-jta_1.1_spec-1.1.1.jar;F:\repository\lib\org\jboss\jandex\2.0.0.Final\jandex-2.0.0.Final.jar;F:\repository\lib\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;F:\repository\lib\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;F:\repository\lib\org\hibernate\common\hibernate-commons-annotations\5.0.1.Final\hibernate-commons-annotations-5.0.1.Final.jar;F:\repository\lib\org\slf4j\slf4j-simple\1.7.5\slf4j-simple-1.7.5.jar;F:\repository\lib\org\slf4j\slf4j-api\1.7.5\slf4j-api-1.7.5.jar;F:\repository\lib\junit\junit\4.11\junit-4.11.jar;F:\repository\lib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.hibernateTest.HibernateTest,test
七月 03, 2019 6:17:20 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.12.Final}
七月 03, 2019 6:17:20 下午 org.hibernate.cfg.Environment
INFO: HHH000206: hibernate.properties not found
七月 03, 2019 6:17:20 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
七月 03, 2019 6:17:20 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 03, 2019 6:17:21 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 03, 2019 6:17:21 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernatetest?useUnicode=true;characterEncoding=UTF-8;useSSL=false;serverTimezone=UTC]
七月 03, 2019 6:17:21 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 03, 2019 6:17:21 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 03, 2019 6:17:21 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
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:264)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:727)
at com.hibernateTest.HibernateTest.test(HibernateTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:105)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.(PooledConnections.java:40)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.(PooledConnections.java:19)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254)
... 36 more
Caused by: java.sql.SQLException: The server time zone value '�й���ʱ��' 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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
... 51 more
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���ʱ��' 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)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 55 more
错误信息中始终提到serverTimezone属性,但是我检查了多遍,确认配置了,而且也配置对了,为什么会报错呢?
最后上了趟厕所,回来以后,想到xml文件配置url后边加参数,是不是不支持分号的这种写法,最后找度娘问了一下还真是,最后果断改成xml支持的方式,改为:
jdbc:mysql://localhost:3306/hibernatetest?userUnicode=true;characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
再次测试程序:
程序测试成功;