0 Replies Latest reply on Mar 17, 2019 6:40 PM by zecas

    Wildfly 11 : Identity generation 1 by 1 (no jumps between restarts)

    zecas

      Hi,

       

      I need some opinions on how to change a behavior regarding identity generation.

       

      I have a project which is running on a version of Wildfly 11.

       

      I'm using JPA in the project, with a persistence.xml file similar to the following example:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.1"
          xmlns="http://xmlns.jcp.org/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
          <persistence-unit name="proj-jpa">
              <jta-data-source>java:/jdbc/ProjDataSource</jta-data-source>
              <class>com.package.model.Table1</class>
              <class>com.package.model.Table2</class>
              <class>com.package.model.Table3</class>
              <class>com.package.model.Table4</class>
              <class>com.package.model.Table5</class>
          </persistence-unit>
      </persistence>
      

       

      Every entity has an identity column, with a defined generation type of identity, similar to:

       

      package com.package.model;
      
      @Entity
      public class Table1 implements Serializable {
      
          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          @Column(name="ID")
          private Long id;
      
          // ...
      }
      
      
      
      

       

      Now, the situation is this: whenever wildfly starts, and a first record is inserted, there is a jump between the IDs.

       

      So hibernate should be just pooling some IDs (say 10 at a time), the we may for instance insert 3 records. We stop wildfly and then when it restarts, there are some that are already been used (it starts insertions on ID 11).

       

      As a result, I'll end up with some missing IDs on the tables. For some tables this is not an issue, but there is 1 table for which I would not desire such behavior.

       

      How can I override it, so it will retrieve the next ID only when necessary?

       

      From what I've looked for, this cannot be done in a global setting for all tables, and it cannot be set on persistence.xml (correct me if I'm wrong).

       

      Is the only possibility to use an additional orm.xml file and just set the allocation size for the column?

       

      For instance, using persistence.xml:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.1"
          xmlns="http://xmlns.jcp.org/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
          <persistence-unit name="proj-jpa">
              <mapping-file>META-INF/orm.xml</mapping-file><!-- references the orm.xml file -->
              <jta-data-source>java:/jdbc/ProjDataSource</jta-data-source>
              <class>com.package.model.Table1</class>
              <class>com.package.model.Table2</class>
              <class>com.package.model.Table3</class>
              <class>com.package.model.Table4</class>
              <class>com.package.model.Table5</class>
          </persistence-unit>
      </persistence>
      

       

      Then orm.xml file:

       

      <?xml version="1.0" encoding="UTF-8" ?>
      <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 orm_2_0.xsd"
                       version="2.0">
          <entity class="com.package.model.Table1">
              <attributes>
                  <id name="id">
                      <sequence-generator allocation-size="1" initial-value="1" />
                  </id>
              </attributes>
          </entity>
      </entity-mappings>
      

       

      So what I wanted was to just change the allocation size behavior for a certain column on a certain table. All other settings will be defined by annotations.

       

      The thing is that on the orm.xml example I gave, the "sequence-generator" element would be usable with <generated-value strategy="SEQUENCE"/>, correct?

       

      So how is it possible to achieve what I want with some changes above? Or not possible at all?

       

      Thanks.