Java Hibernate无法创建表Hibernate\u序列
休眠版本:Java Hibernate无法创建表Hibernate\u序列,java,mysql,hibernate,mariadb,Java,Mysql,Hibernate,Mariadb,休眠版本: org.hibernate 冬眠核心 5.2.4.最终版本 ExportDB.java: public class ExportDB { public static void main(String[] args) { ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); Metadata metadata =
org.hibernate
冬眠核心
5.2.4.最终版本
ExportDB.java
:
public class ExportDB {
public static void main(String[] args) {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
}
运行ExportDB.java
:
2016-11-19 00:22:12,845 WARN [org.hibernate.orm.connections.pooling] - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Hibernate: drop table if exists hibernate_sequences
Hibernate: drop table if exists user
Hibernate: create table hibernate_sequences (sequence_name varchar(255) not null, sequence_next_hi_value bigint, primary key (sequence_name))
Hibernate: create table user (id bigint not null, balance decimal(20,4) default 0.00, createTime time, displayName varchar(64), password varchar(64), username varchar(64), primary key (id))
Hibernate: alter table user add constraint UK_7kuje5s4lbyq9qyv1r9ecm2it unique (username)
数据库:
MariaDB[cms]>显示表格;
+----------------+
|表\u在\u cms中|
+----------------+
|投资者|
+----------------+
一行一组(0.00秒)
当我使用打印SQL创建hibernate\u序列时
:
MariaDB [cms]> create table hibernate_sequences (sequence_name varchar(255) not null, next_val bigint, primary key (sequence_name));
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
如何使
ExportDB.java
可以创建hibernate\u序列
?您正在使用utf8mb4
,对吗?您使用的是5.6或更高版本
计划A:升级到5.7
方案B:将255减少到191或更少。(你真的需要255??)
方案C:更改为字符集utf8
(假设您不需要表情符号或中文)
方案D:“序列”通常是数字。如果是这样的话,
INT UNSIGNED
或BIGINT UNSIGNED
不会起作用吗?这是因为在我的情况下,列定义上的utf8mb4
字符集
在主键中不能使用太长的varchar列
因此,我们可以手动创建表并使其正确
下面的CREATETABLE查询实现了这一点
create table hibernate_sequences (
sequence_name varchar(255) CHARACTER SET utf8 not null ,
next_val bigint,
primary key (sequence_name)
) engine=MyISAM
您的变量
sequence\u name
似乎太大,无法用作主键。看看这个问题:255个字符(每个字符3个字节)是765个字节,再加上一些额外的字节是必需的,所以它超过了767的限制。谢谢,但是如何更改为utf8呢?你是说整个数据库?altertable。。。对于任何需要更改的表,请转换为…
。更多讨论: