'It's seems hibernate ignores allocationSize

I'm trying different hibernate id generation strategies and I've noticed that it seems like hibernate ignores allocationSize.

I have the following entity class:

@Entity
@TableGenerator(name = "id_gen", allocationSize = 100)
public class Book {
    @Id
    @GeneratedValue(generator = "id_gen")
    private long id;
    private String title;
}

When I run the following code:

em.persist(book); //id is 0
em.persist(book1); // id is 0

This is what gets logged:

Hibernate: 
    select
        tbl.next_val 
    from
        id_gen tbl 
    where
        tbl.sequence_name=? for update
Hibernate: 
    update
        id_gen 
    set
        next_val=?  
    where
        next_val=? 
        and sequence_name=?
Hibernate: 
    select
        tbl.next_val 
    from
        id_gen tbl 
    where
        tbl.sequence_name=? for update
Hibernate: 
    update
        id_gen 
    set
        next_val=?  
    where
        next_val=? 
        and sequence_name=?

In my entity class the allocationSize is set to 100, but as you can see in the logs hibernate executes a sql query to get the next id every time.

Now I don't understand what is going on exactly. Am I doing something wrong? do I need more config? or maybe this log does not really get executed in the database. could someone please clarify this?

By the way, I'm using h2 database.

Edit

persistence.xml:

<properties>
            <property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:h2:~/library;DB_CLOSE_ON_EXIT=FALSE"/>
            <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
            <property name="hibernate.show_sql" value = "true" />
            <property name="hibernate.format_sql" value="true"/>
        </properties>

DDL Logs:

Hibernate: 
    
    drop table if exists Book cascade 
Hibernate: 
    
    drop table if exists id_gen cascade 
Hibernate: 
    
    create table Book (
       id bigint not null,
        title varchar(255),
        primary key (id)
    )

Hibernate: 
    
    create table id_gen (
       sequence_name varchar(255) not null,
        next_val bigint,
        primary key (sequence_name)
    )
Hibernate: 
    
    insert into id_gen(sequence_name, next_val) values ('Book',0)

H2 tables:

book table id_gen table



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source