Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate_sequence table is generated

I have id column with generated strategy AUTO, I'm wondering, why MySql generate hibernate_sequence table? I supposed that hibernate will pick IDENTITY id generating strategy

<mapped-superclass class="com.cl.xlp.model.data.Identity">
    <attributes>
        <id name="id">
            <column name="id" />
            <generated-value strategy="AUTO" />
        </id>
    </attributes>
</mapped-superclass>

Hibernate properties

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update

Mysql connector version

version.mysql.connector>5.1.39</version.mysql.connector>

Mysql server version is 5.6.12

like image 451
Vitalii Kravchenko Avatar asked Jan 04 '17 10:01

Vitalii Kravchenko


People also ask

Why hibernate_sequence is created?

Why hibernate_sequence is created? hibernate_sequence' doesn't exist error occurs when the hibernate_sequence table does not exist in the database and auto id generation in hibernate is enabled. Hibernate generates and stores the current id for auto-increment column values in the hibernate sequence table.

How to avoid Hibernate_ sequence table?

By default, Hibernate generates key from hibernate_sequence table, we can disable it by setting this hibernate. use-new-id-generator-mappings to false.


1 Answers

The way Hibernate interprets AUTO generation type has changed starting with Hibernate version 5.0.

When using Hibernate v 4.0 and Generation Type as AUTO, specifically for MySql, Hibernate would choose the IDENTITY strategy (and thus use the AUTO_INCREMENT feature) for generating IDs for the table in question.

Starting with version 5.0 when Generation Type is selected as AUTO, Hibernate uses SequenceStyleGenerator regardless of the database. In case of MySql Hibernate emulates a sequence using a table and is why you are seeing the hibernate_sequence table. MySql doesn't support the standard sequence type natively.

References

  • http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-auto
  • https://www.thoughts-on-java.org/5-things-you-need-to-know-when-using-hibernate-with-mysql/
like image 164
Kamalesh Patil Avatar answered Sep 22 '22 18:09

Kamalesh Patil