Class SequenceIdGenerator

java.lang.Object
io.github.blyznytsiaorg.bibernate.dao.jdbc.identity.AbstractGenerator
io.github.blyznytsiaorg.bibernate.dao.jdbc.identity.SequenceIdGenerator
All Implemented Interfaces:
Generator

public class SequenceIdGenerator extends AbstractGenerator implements Generator
Implementation of the sequence-based ID generator in Bibernate.

This generator is responsible for handling entities with sequence-based generation of primary key values. This SequenceIdGenerator generator anticipates that the identifier will be generated in additional query to database before an INSERT operation into the table.

Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

  • Constructor Details

    • SequenceIdGenerator

      public SequenceIdGenerator(BibernateDatabaseSettings bibernateDatabaseSettings, List<String> executedQueries)
      Constructs an SequenceIdGenerator with the specified Bibernate database settings and a list to store executed queries.
      Parameters:
      bibernateDatabaseSettings - The database settings for Bibernate.
      executedQueries - The list to store executed queries.
  • Method Details

    • type

      public GenerationType type()
      Returns SEQUENCE GenerationType for further understanding of what type of generator we are dealing with
      Specified by:
      type in interface Generator
      Returns:
      the generation type
    • handle

      public <T> void handle(Class<T> entityClass, Collection<T> entities, DataSource dataSource)
      Handles the generation of sequence-based primary keys for a collection of entities and inserts the entities into the database using batch processing. Gets generated id from the sequence of database and sets it to the entity. If the method will be performed in transaction then id will be cleaned up from the entity in case of rollback. Can overbook set of sequences (depends on allocation size property) and use it to avoid redundant sql queries to database
      Specified by:
      handle in interface Generator
      Type Parameters:
      T - The type of entities in the collection.
      Parameters:
      entityClass - The class of the entities being handled.
      entities - The collection of entities for which primary keys are generated.
      dataSource - The data source for obtaining a database connection.
    • generateId

      private Object generateId(Class<?> entityClass, String tableName, DataSource dataSource)
    • getNextIdFromDbSeq

      private Long getNextIdFromDbSeq(DataSource dataSource, String sequenceName)
    • getSequenceConf

      private SequenceConf getSequenceConf(Class<?> entityClass, String tableName)