简体   繁体   English

Hibernate无法创建表hibernate_sequences

[英]Hibernate cannot create table hibernate_sequences

Hibernate version: 休眠版本:

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

ExportDB.java : 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);
    }
}

Run ExportDB.java : 运行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)

Database: 数据库:

MariaDB [cms]> show tables;
+----------------+
| Tables_in_cms |
+----------------+
| investor       |
+----------------+
1 row in set (0.00 sec)

When I use printed SQL to create hibernate_sequences : 当我使用打印的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

How can I make ExportDB.java can create hibernate_sequences ? 如何使ExportDB.java可以创建hibernate_sequences

You are using utf8mb4 , correct? 您正在使用utf8mb4 ,对吗? And you are using 5.6 or older? 而您使用的是5.6或更早版本?

Plan A: Upgrade to 5.7. 计划A:升级到5.7。

Plan B: Decrease 255 to 191 or less. 方案B:将255减少至191或更少。 (Did you really need 255??) (您真的需要255吗?)

Plan C: Change to CHARACTER SET utf8 (assuming you don't need Emoji or Chinese) 方案C:更改为CHARACTER SET utf8 (假设您不需要表情符号或中文)

Plan D: Normally a 'sequence' is something numeric. 计划D:通常,“序列”是数字。 If that is the case, won't INT UNSIGNED or BIGINT UNSIGNED work? 如果是这种情况, INT UNSIGNEDBIGINT UNSIGNED不会起作用吗?

It is due to the utf8mb4 charset on the column definition in my case. 在我的情况下,这是由于列定义上utf8mb4字符集。

Too long varchar column cannot be used in primary_key. varchar列太长,无法在primary_key中使用。

So we can manually create the table and make it correct. 因此,我们可以手动创建表并使其正确。

The following create table query do the trick. 以下创建表查询可以解决问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM