2015-09-06 32 views
9

Così ho scaricato Hibernate 5.0.0.1 e ho provato il mio progetto, che in precedenza utilizzava ibernazione 4.3.hibernate crea una tabella vuota - hibernate_sequence all'avvio

Quando inserisco nel database, mi dà questo errore:

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

Sto usando mysql, e la mia strategia di generazione è fissato a GenerationType.auto, e sembra che ora ibernare pensa utilizzando le sequenze è la migliore strategia per generare valori. Ma il tavolo è vuoto. Penso che ibernare stia tentando di ottenere un valore dalla sequenza ma non ne trovi. Ma sono confuso perché hibernate_sequence viene creato da hibernate, non dovrebbe fornire un valore iniziale?

risposta

15

La tabella di sequenza è a causa di come hai definito la chiave primaria su una/tutte le tue entità.

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

Se si desidera utilizzare colonna di identità di una tabella:

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

Si potrebbe per controllare questo thread per maggiori informazioni: 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

ma la mia strategia è impostata su auto, e penso che lo farei usare ciò che l'ibernazione pensa sia la migliore strategia. Ma come posso popolare la sequenza attraverso l'ibernazione? – morbidCode

+2

L'ultimo elemento nella citazione sopra dice tutto. Onestamente, non utilizzare AUTO a meno che IDENTITY non funzioni per te. La maggior parte dei database supporta identificatori univoci per tabella. –

1

Simple solution :

CREATE TABLE hibernate_sequence come :

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

è creato perché Hibernate usando quel tavolo per tenere traccia di incremento automatico Id (Avanti Valore della ID)

Ex-

@Id 
@GeneratedValue 
int id;