HIbernate 5: generator class=“sequence” not working

旧时模样 提交于 2020-01-01 03:04:08

问题


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.


回答1:


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>
    



回答2:


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);



回答3:


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

That worked for me.



来源:https://stackoverflow.com/questions/32978645/hibernate-5-generator-class-sequence-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!