Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate creating empty table - hibernate_sequence at startup

So I just downloaded hibernate 5.0.0.1, and I tried my project to it, which is previously using hibernate 4.3.

When I insert to the database, it gives me this error:

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

I am using mysql, and my generation strategy is set at GenerationType.auto, and it seems that now hibernate thinks using sequences is the best strategy for generating values. But the table is empty. I think hibernate is atempting to get a value from the sequence but can't find any. But I'm confused because the hibernate_sequence is created by hibernate, shouldn't it provide an initial value?

like image 844
lightning_missile Avatar asked Sep 06 '15 14:09

lightning_missile


1 Answers

The sequence table is because of how you've defined the primary key on one/all of your entities.

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

If you want to use a table's identity column:

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

You might to check this thread for more information: 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.

like image 168
John Manko Avatar answered Oct 02 '22 20:10

John Manko