HIbernate 5: generator class="sequence" not working

Green Root picture Green Root · Oct 6, 2015 · Viewed 8.4k times · Source

I have following mapping:

    <id name="id" type="java.lang.Long" column="id">
        <generator class="sequence">
            <param name="sequence">tracksdata_seq</param>
        </generator>
    </id>

Everything went fine when I worked with it in Hibernate 4.2. Now I am migrating to Hibernate 5 and facing following issue:

2015-10-06 19:49:50 DEBUG SQL:92 - select nextval ('hibernate_sequence')
2015-10-06 19:49:50 DEBUG SqlExceptionHelper:122 - could not extract ResultSet [n/a]
org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist

How to resolve this issue?

P.S. Hibernate 5.0.2.Final.

Answer

Mark Ola picture Mark Ola · Jan 4, 2016

You have two options:

  1. You set the hibernate.id.new_generator_mappings configuration property to false and switch back to the old identifier generators
  2. You change the mapping as follows, from this:

    <generator class="sequence">
        <param name="sequence">MY_SEQUENCE</param>
    </generator>
    

    to:

    <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
        <param name="optimizer">none</param>
        <param name="increment_size">1</param>
        <param name="sequence_name">MY_SEQUENCE</param>
    </generator>