Hibernate 6 Migration - Id Generation

have a base entity. I have a sequence per entity strategy. Increment size is 100. As per documentation in hibernate 6, default strategy is sequence per entity. But i couldn’t find a way to set default increment size to 100.

How i can migrate the code below to work with hibernate 6.


@Getter
@Setter
@MappedSuperclass
@Audited
public abstract class AbstractEntity {

    @Id
    @GeneratedValue(generator = "optimized-sequence")
    @GenericGenerator(
        name = "optimized-sequence",
        strategy = "enhanced-sequence",
        parameters = {
            @Parameter(name = SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, value = "true"),
            @Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "100")})
    private Long id;

I don’t think there is a way to configure a global default increment size yet. Try replacing the @GenericGenerator with @SequenceGenerator(name = "optimized-sequence", allocationSize = 100). If that doesn’t work, you can create a feature request for this if you want. In the meantime, you will have to define every sequence generator on the respective entity types by annotating e.g. @SequenceGenerator(name = "optimized-sequence", sequenceName = "abc_seq", allocationSize = 100)

It doesn’t work.

Since it is our base entity, i don’t want to move it from base to every entity. We have a lot of entities.

Then there is no other way right now. Please create a JIRA issue for the feature request.

Is anything for this issue have been implemented?

Just wanted to chime in here that we found a (maybe not optimal) solution for this issue. We also had a global @GenericGenerator that we needed to replace after migrating. We also did not want to move this to every Entity. Hibernate Sequence generator actually does what we wanted, but has a default increment size of 50, and we had 1 before. Since we also did not want to migrate every sequence to use this increment size (yet), we needed another solution. What seems to work fine is to actually just do what the migration steps for @GenericGenerator suggest.

@IdGeneratorType(CustomSequenceGenerator.class)
@Retention(RUNTIME)
@Target({METHOD, FIELD})
public @interface OptimizedSequence {
    int incrementBy() default 1;
}

And:

public class CustomSequenceGenerator extends SequenceStyleGenerator implements BeforeExecutionGenerator {

    private final int incrementSize;

    public CustomSequenceGenerator(OptimizedSequence config) {
        this.incrementSize = config.incrementBy();
    }

    @Override
    protected int determineIncrementSize(Properties params) {
        return incrementSize;
    }
}

This just extends the SequenceStyleGenerator of Hibernate and overwrites the determineIncrementSize function to provide another default.

So far this is working fine and we did not encounter issues, but i am not sure how stable this will be for upgrades in the future.