Hibernate无法创建表hibernate_sequences

时间:2016-11-18 16:58:10

标签: java mysql hibernate mariadb

Hibernate版本:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.4.Final</version>
</dependency>

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]> show tables;
+----------------+
| Tables_in_cms |
+----------------+
| investor       |
+----------------+
1 row in set (0.00 sec)

当我使用打印的SQL创建hibernate_sequences时:

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_sequences

2 个答案:

答案 0 :(得分:1)

您正在使用utf8mb4,对吗?你使用的是5.6或更老版本吗?

计划A:升级到5.7。

计划B:减少255到191或更少。 (你真的需要255吗?)

计划C:更改为CHARACTER SET utf8(假设您不需要表情符号或中文)

计划D:通常是一个&#39;序列&#39;是数字的东西。如果是这种情况,请不要INT UNSIGNEDBIGINT UNSIGNED工作?

答案 1 :(得分:0)

这是由于我的情况下列定义上的utf8mb4字符集

在primary_key中不能使用太长的varchar列。

因此我们可以手动创建表并使其正确。

以下创建表查询可以解决问题。

create table hibernate_sequences (
   sequence_name varchar(255) CHARACTER SET utf8 not null ,
    next_val bigint,
    primary key (sequence_name)
) engine=MyISAM