9

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.

Bacteria
  • 8,406
  • 10
  • 50
  • 67
Green Root
  • 644
  • 2
  • 10
  • 28
  • It is looking for sequence hibernate_sequence, did you mention that in the mapping above? You have tracksdata_seq, not sure if that is refering to hyberante_sequence – Zeus Oct 14 '15 at 20:23
  • This should not be referring to hibernate_sequence - it must use tracksdata_seq, like it was in hibernate 4.2. – Green Root Oct 15 '15 at 05:04
  • @maksim2020 I had the same issue migrating from 4.3.8 to 5.0.6Final. This only happens if you use XML mappings. I switched to JPA annotations and it worked fine. – Gustavo Dec 29 '15 at 20:30
  • 1
    Notice that the param name is no longer "sequence", rather as of version 5 it is "sequence_name". See http://stackoverflow.com/questions/42191210/why-does-hibernate-5-sequence-generator-use-the-default-hibernate-seq-instead-of – Vering Apr 19 '17 at 11:06

3 Answers3

16

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>
    
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Mark Ola
  • 305
  • 2
  • 12
  • Spring boot format: spring.jpa.properties.hibernate.id.new_generator_mappings. But I also needed spring.jpa.properties.hibernate.default_schema for hibernate to use the correct schema name – Jay Aug 13 '18 at 11:08
3

Use "sequence_name" instead of "sequence" in <param name="sequence">.

That worked for me.

Clijsters
  • 4,031
  • 1
  • 27
  • 37
Pavan Ghantasala
  • 209
  • 1
  • 4
  • 13
  • The class="sequence" generator was deprecated, so probably worthwhile changing it as specified in other answers, but this change is easier in the meantime, thanks! – shoguren Mar 17 '21 at 23:53
1

I also encountered this problem when migrating from Hibernate 4.3.10 to Hibernate 5.0.4. Like maksim2020, I replaced instances of <generator class="sequence"> with <generator class="identity">. However, to preserve the id sequence for the affected tables I also had to write a sql migration script which set the default value for the column to be the next value of the existing sequence. In PostgreSQL this is done as follows:

ALTER TABLE ONLY affected_table ALTER COLUMN affected_id SET DEFAULT nextval('original_sequence'::regclass);
pants
  • 192
  • 13