2015-09-06 16 views
9

Więc właśnie pobrałem hibernate 5.0.0.1, i wypróbowałem na nim mój projekt, który poprzednio korzystał z hibernacji 4.3.hibernacja tworzy pustą tabelę - hibernate_sequence na starcie

Po włożeniu do bazy danych, to daje mi ten błąd:

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

używam MySQL i moja strategia generacja jest ustawiony na GenerationType.auto i wydaje się, że teraz hibernacji myśli, używając sekwencji jest najlepsza strategia generowania wartości. Ale stół jest pusty. Myślę, że hibernacja jest próbą uzyskania wartości z sekwencji, ale nie może jej znaleźć. Ale jestem zdezorientowany, ponieważ hibernacja_sekwencji jest tworzona przez hibernację, czy nie powinna zapewnić wartości początkowej?

Odpowiedz

15

Tabela sekwencji dotyczy tego, jak zdefiniowałeś klucz podstawowy na jednym/wszystkich twoich obiektach.

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

Jeśli chcesz użyć kolumny tożsamości stole za:

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

Mógłbyś sprawdzić ten wątek, aby uzyskać więcej informacji: 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

ale moja strategia jest ustawione na auto, i myślę, że byłoby wykorzystaj to, co hibernacja myśli, jest najlepszą strategią. Ale jak zapełnić sekwencję przez hibernację? – morbidCode

+2

Ostatni element cytatu powyżej mówi wszystko. Szczerze mówiąc, nie używaj AUTO, chyba że TOŻSAMOŚĆ nie działa dla ciebie. Większość baz danych obsługuje unikalne identyfikatory dla tabeli. –

1

Simple solution :

utworzyć tabelę hibernate_sequence jako :

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

Jest tworzony ponieważ Hibernacja przy użyciu tej tabeli do śledzenia automatycznego przyrostu ID (następna wartość identyfikatora)

Ex

@Id 
@GeneratedValue 
int id; 
Powiązane problemy