Hibernate常见问题及其解决方案

1.以下问题的前提是Hibernate实体类,映射文件,主配置文件格式都符合要求,否则毫无意义

1.数据连接报错
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
提示创建jdbc报错,环境存在异常,但这个报错并没有指出问题所在,重点应当看这个报错后面的报错:
1.1Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol
mysql版本和驱动版本不匹配,我的mysql是8.0.21版本
在mysql中查询版本的语句是:    select VERSION()

Hibernate常见问题及其解决方案

1.2.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.、
这个乱码异常提示让我们配置serverTimezone,这个参数是区时
serverTimezone=UTC    全球标准时间
serverTimezone=GMT%2B8    东八区(北京时间)
serverTimezone=Asia/Shanghai    上海时间,和北京时间二选一即可

1.3.如果含有多个参数,一定不能用&隔开,要用&隔开,注意分号不能丢掉,否则也会报错,提示&号相关的报错

 

2.其他报错
org.hibernate.HibernateException: The database returned no natively generated identity value
数据库的表主键没有设置默认值+自增长,这样的情况,可以手动配置,也可以使用Hibernate的自动建表