美文网首页
解决springboot+jpa+hibernate启动时报错:

解决springboot+jpa+hibernate启动时报错:

作者: HeWorldZH | 来源:发表于2018-03-21 12:03 被阅读0次

错误信息:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes

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.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.Util.getInstance(Util.java:408)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2483)

at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)

at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)

at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)

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.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)

at com.sun.proxy.$Proxy111.executeUpdate(Unknown Source)

at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56)

... 37 common frames omitted

原因:

开启hibernate的sql语句显示可以看到hibernate在帮我们创建表的时候,字符串类型用的是varchar(255),而mysql支持的最大varchar长度是255.666 ,但前提条件是要用utf8字符集,也就是mysql的varchar最大支持767个字节(一个utf8=3个字节),但不知是什么原因,默认情况下hibernate用varchar(255)会报上面的错误(猜测:可能hibernate默认用的不是utf8而是其他类似于utf16这样的大于3字节的字符集)

解决方法:

在数据库的连接配置的url上手动指定采用utf8字符集(jdbc:mysql://localhost:3306/doll_paidb?&useUnicode=true&characterEncoding=utf8)

对应springboot 的yaml配置文件写法如下:

spring:

datasource:

username: helll

password: xxxx123456

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://server.yidou360.com:3306/doll_pai_erp_basic?&useUnicode=true&characterEncoding=utf-8

然后修改数据库字符集,mysql命令:alter database mydb character set utf-8;

相关文章

网友评论

      本文标题:解决springboot+jpa+hibernate启动时报错:

      本文链接:https://www.haomeiwen.com/subject/qwevqftx.html