2015-09-06 16 views
9

Vì vậy, tôi vừa tải xuống hibernate 5.0.0.1 và tôi đã thử dự án của mình với nó, trước đây sử dụng ngủ đông 4.3.hibernate tạo bảng trống - hibernate_sequence khi khởi động

Khi tôi chèn vào cơ sở dữ liệu, nó mang lại cho tôi lỗi này:

ERROR: could not read a hi value - you need to populate the table: hibernate_sequence

Tôi đang sử dụng mysql, và chiến lược thế hệ chúng tôi được đặt ở GenerationType.auto, và có vẻ như rằng bây giờ ngủ đông cho rằng sử dụng các trình tự được chiến lược tốt nhất để tạo ra giá trị. Nhưng cái bàn trống. Tôi nghĩ rằng hibernate là atempting để có được một giá trị từ trình tự nhưng không thể tìm thấy bất kỳ. Nhưng tôi bối rối vì hibernate_sequence được tạo bởi hibernate, không nên nó cung cấp một giá trị ban đầu?

Trả lời

15

Bảng tuần tự là do cách bạn đã xác định khóa chính trên một/tất cả các thực thể của bạn.

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) // or GenerationType.SEQUENCE 
protected Long id; 

Nếu bạn muốn sử dụng cột sắc của bảng:

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
protected Long id; 

Bạn có thể để kiểm tra chủ đề này để biết thêm thông tin: https://forums.hibernate.org/viewtopic.php?f=1&t=999785&start=0

@GeneratedValue JPA Annotation

Quite often in these tutorials, we have used the @GeneratedValue annotation to have thedatabase generate a unique primary key for us. We have used the default Generation Type in each of our examples, but there are actually four different strategies for having the primary key generated by the database. Those four options are:

AUTO IDENTITY TABLE SEQUENCE javax.persistence.GenerationType.AUTO

The AUTO generation strategy is the default, and this setting simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY, although it might be TABLE or SEQUENCE depending upon how the database is configured. The AUTO strategy is typically recommended, as it makes your code and your applications most portable.

javax.persistence.GenerationType.IDENTITY

The IDENTITY option simply allows the database to generate a unique primary key for your application. No sequence or table is used to maintain the primary key information, but instead, the database will just pick an appropriate, unique number for Hibernate to assign to the primary key of the entity. With MySQL, the first lowest numbered primary key available in the table in question is chosen, although this behavior may differ from database to database.

javax.persistence.GenerationType.Sequence

Some database vendors support the use of a database sequence object for maintaining primary keys. To use a sequence, you set the GenerationType strategy to SEQUENCE, specify the name of the generator annotation, and then provide the @SequenceGenerator annotation that has attributes for defining both the name of the sequence annotation, and the name of the actual sequence object in the database.

+1

nhưng chiến lược của tôi được thiết lập để tự động, và tôi nghĩ rằng tôi sẽ sử dụng những gì hibernate nghĩ là chiến lược tốt nhất. Nhưng làm thế nào tôi có thể cư trú trình tự thông qua hibernate? – morbidCode

+2

Mục cuối cùng trong câu nói trên nói lên tất cả. Thành thật mà nói, không sử dụng AUTO trừ khi IDENTITY không hoạt động cho bạn. Hầu hết các cơ sở dữ liệu hỗ trợ các mã định danh duy nhất trên mỗi bảng. –

1

Simple solution :

tạo bảng hibernate_sequence như :

"create sequence <schema/db>.hibernate_sequence" 
1

Nó được tạo bởi vì Hibernate sử dụng bảng để theo dõi tự động tăng Id (Tiếp theo Giá trị của ID)

Ex-

@Id 
@GeneratedValue 
int id; 
Các vấn đề liên quan