HIbernate 5: generator class="sequence" not working
Asked Answered
G

3

10

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.

Geibel answered 6/10, 2015 at 20:1 Comment(4)
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_sequenceOveract
This should not be referring to hibernate_sequence - it must use tracksdata_seq, like it was in hibernate 4.2.Geibel
@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.Orvil
Notice that the param name is no longer "sequence", rather as of version 5 it is "sequence_name". See #42191710Bruch
S
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>
    
Senarmontite answered 4/1, 2016 at 9:27 Comment(1)
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 nameAbhor
P
6

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

That worked for me.

Peroxidize answered 27/7, 2017 at 17:52 Comment(1)
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!Turn
A
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);
Alongshore answered 23/11, 2015 at 5:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.