2016-11-18 121 views
1

Hibernate的版本:Hibernate無法創建表hibernate_sequences

<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

+0

看來你的變量'sequence_name'太大而不能用作主鍵。看看這個問題:http://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-767-bytes每個字符3個字節的255個字符是765字節,加上一些額外的字節是必需的,所以它超過了767的限制。 – Enwired

回答

1

您正在使用utf8mb4,對嗎?你使用的是5.6或更高版本?

計劃A:升級到5.7。

計劃B:減少255至191或更少。 (你真的需要255?)

計劃C:更改爲CHARACTER SET utf8(假設你不需要的表情符號或中國)

計劃d:正常情況下,「序」是什麼數字。如果是這樣的話,會不會INT UNSIGNEDBIGINT UNSIGNED工作?

+0

謝謝,但如何更改爲utf8?你的意思是整個數據庫? –

+0

'ALTER TABLE ... CONVERT TO ...'需要更改任何表格。更多討論:http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes –

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