4 Replies Latest reply on Apr 28, 2017 5:52 AM by valsaraj viswanathan

    Hibernate sequence generator not working as expected

    valsaraj viswanathan Master

      Hi,

       

      I migrated application from JBoss 4.2.2 GA to WildFly-8.2.0. Hibernate sequence generator not working as expected in WildFly. It was working fine in old version. Here is the details:

       

      Entity:

          @Id

          @GeneratedValue(strategy=GenerationType.TABLE, generator="SAMPLE_SEQ_GEN")

          private long id;

       

       

      orm.xml:

      <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm/orm_2_0.xsd" version="2.0">

      <table-generator name="SAMPLE_SEQ_GEN" table="SEQUENCE_GENERATOR" schema="sample" pk-column-name="SEQ_NAME" value-column-name="NEXT_VAL" pk-column-value="SAMPLE" initial-value="1000" allocation-size="1000">

      <unique-constraint>

      <column-name>SEQ_NAME</column-name>

      </unique-constraint>

      </table-generator>

       

      From log:

      [org.hibernate.Version] (ServerService Thread Pool -- 71) HHH000412: Hibernate Core {4.3.7.Final}

      .....

      [org.hibernate.validator.internal.util.Version] (MSC service thread 1-2) HV000001: Hibernate Validator 5.1.3.Final

      .....

      [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 72) HCANN000001: Hibernate Commons Annotations {4.0.4.Final}

       

      The id column value is sometimes negative value or less than 1000. It is not expected as per initial-value="1000" allocation-size="1000".

       

      Thanks!

        • 1. Re: Hibernate sequence generator not working as expected
          Scott Marlow Master

          Read about the Hibernate settings that might be important related to your issue here https://docs.jboss.org/author/display/WFLY8/JPA+Reference+Guide#JPAReferenceGuide-Hibernateproperties

           

          By default, JPA applications will deploy with hibernate.id.new_generator_mappings=true, while pure Hibernate applications will deploy with hibernate.id.new_generator_mappings=false.


          Quoting from the above:

           

          In Hibernate 4.x, if new_generator_mappings is true:

          • @GeneratedValue(AUTO) maps to org.hibernate.id.enhanced.SequenceStyleGenerator
          • @GeneratedValue(TABLE) maps to org.hibernate.id.enhanced.TableGenerator
          • @GeneratedValue(SEQUENCE) maps to org.hibernate.id.enhanced.SequenceStyleGenerator

            In Hibernate 4.x, if new_generator_mappings is false:

          • @GeneratedValue(AUTO) maps to Hibernate "native"
          • @GeneratedValue(TABLE) maps to org.hibernate.id.MultipleHiLoPerTableGenerator
          • @GeneratedValue(SEQUENCE) to Hibernate "seqhilo"

           

          In summary, I think that you used to use org.hibernate.id.MultipleHiLoPerTableGenerator (for TABLE) and now are getting org.hibernate.id.enhanced.TableGenerator.  You could try setting hibernate.id.new_generator_mappings=false in your persistence.xml (as a persistence unit property).


          Scott

          1 of 1 people found this helpful
          • 2. Re: Hibernate sequence generator not working as expected
            valsaraj viswanathan Master

            Hi Scott,

             

            Here is the persistence.xml file of my JPA application:

            <?xml version="1.0" encoding="UTF-8"?>

            <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">

            <persistence-unit name="em" transaction-type="JTA">

            <jta-data-source>java:jboss/datasources/AppDS</jta-data-source>

            <mapping-file>META-INF/orm.xml</mapping-file>

            <properties>

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>

            <property name="hibernate.show_sql" value="false"></property>

            <property name="hibernate.hbm2ddl.auto" value="update"></property>

            </properties>

            </persistence-unit>

            </persistence>

             

            Should I add setting hibernate.id.new_generator_mappings=false here?

             

            Thanks!

            • 3. Re: Re: Hibernate sequence generator not working as expected
              Scott Marlow Master

              Yes, here is an example that you could try.

               

              <?xml version="1.0" encoding="UTF-8"?>

              <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">

              <persistence-unit name="em" transaction-type="JTA">

              <jta-data-source>java:jboss/datasources/AppDS</jta-data-source>

              <mapping-file>META-INF/orm.xml</mapping-file>

              <properties>

              <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>

              <property name="hibernate.show_sql" value="false"></property>

              <property name="hibernate.id.new_generator_mappings" value="false"></property>

              <property name="hibernate.hbm2ddl.auto" value="update"></property>

              </properties>

              </persistence-unit>

              </persistence>

              • 4. Re: Hibernate sequence generator not working as expected
                valsaraj viswanathan Master

                Hi Scott,

                 

                I checked this in WildFly 10.1. It is working fine with hibernate.id.new_generator_mappings=false but there is warning in log.

                 

                HHH90000015: Found use of deprecated [org.hibernate.id.MultipleHiLoPerTableGenerator] table-based id generator; use org.hibernate.id.enhanced.TableGenerator instead. 

                See Hibernate Domain Model Mapping Guide for details.

                ...

                2017-04-27 17:52:01,422 WARN  [org.hibernate.orm.deprecation] (ServerService Thread Pool -- 76)

                HHH90000014: Found use of deprecated [org.hibernate.id.SequenceGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead. 

                See Hibernate Domain Model Mapping Guide for details.

                 

                When I set hibernate.id.new_generator_mappings=true, warning disappeared but the table id generated is not as in previous order. It's creating id with negative values.

                 

                Any option to solve this?

                 

                Thanks!