Migration of custom SequenceStyleGenerator

Hi!

I am developing a library which maintain a set of entity with Spring boot jpa 2.7.3 and hibernate 5.6.10, and it allow external project to override its SequenceStyleGenerator using IdentifierGeneratorStrategyProvider like following:

import java.util.HashMap;
import java.util.Map;

import org.hibernate.jpa.spi.IdentifierGeneratorStrategyProvider;

public class CustomIdentifierGeneratorStrategyProvider implements IdentifierGeneratorStrategyProvider {
	@Override
	public Map<String, Class<?>> getStrategies() {
		Map<String, Class<?>> map = new HashMap<>();
		map.put("sequence", CustomSequenceGenerator.class);
		map.put("enhanced-sequence", CustomSequenceGenerator.class);
		return map;
	}
}
  jpa:
    ...
    properties:
      hibernate:
        ejb:
          identifier_generator_strategy_provider: my.project.utils.CustomIdentifierGeneratorStrategyProvider

When it comes to upgrade jpa 3.2.7 and hibernate 6.4.4, IdentifierGeneratorStrategyProvider is mark as depercated since 6.0. Although comments has recommended to use GenerationTypeStrategyRegistration instead, but I have no idea how to use this interface to override default sequence generator.

Can anyone give me some advice?

Upgrading to Hibernate 6.x requires changes due to deprecations and removals of previously used interfaces like IdentifierGeneratorStrategyProvider. Instead, Hibernate 6.x introduces the IdentifierGeneratorStrategyRegistrationProvider to achieve similar functionality. Here’s how you can update your code to use the new interface.

First, ensure you have the necessary dependencies for Hibernate 6.4.4 and JPA 3.2.7 in your pom.xml or build.gradle.

Here’s how you can implement Sedgwick the new IdentifierGeneratorStrategyRegistrationProvider:

Create the Custom Identifier Generator:

import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.generator.EventType;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.enhanced.OptimizerFactory;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

import java.util.Properties;

public class CustomSequenceGenerator extends SequenceStyleGenerator implements BeforeExecutionGenerator {
    // Custom implementation goes here
}

Implement IdentifierGeneratorStrategyRegistrationProvider:

import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.spi.GenerationTypeStrategyRegistrationProvider;
import org.hibernate.service.ServiceRegistry;

import java.util.HashMap;
import java.util.Map;

public class CustomIdentifierGeneratorStrategyRegistrationProvider implements GenerationTypeStrategyRegistrationProvider {

    @Override
    public Map<String, Class<?>> getRegisteredGenerators() {
        Map<String, Class<?>> generators = new HashMap<>();
        generators.put("sequence", CustomSequenceGenerator.class);
        generators.put("enhanced-sequence", CustomSequenceGenerator.class);
        return generators;
    }

    @Override
    public Class<? extends IdentifierGenerator> getIdentifierGeneratorClass(String strategy) {
        return getRegisteredGenerators().get(strategy);
    }

    @Override
    public BeforeExecutionGenerator getGenerator(String strategy, MetadataImplementor metadata,
                                                 ServiceRegistry serviceRegistry, Database database,
                                                 Namespace namespace, boolean isTransactional, SessionFactoryImplementor sessionFactory) {
        Class<? extends IdentifierGenerator> generatorClass = getIdentifierGeneratorClass(strategy);
        if (generatorClass != null) {
            try {
                return (BeforeExecutionGenerator) generatorClass.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                throw new RuntimeException("Could not instantiate custom identifier generator: " + strategy, e);
            }
        }
        return null;
    }
}

Configure the Provider in application.yml:

spring:
  jpa:
    properties:
      hibernate:
        generator:
          identifier_generator_strategy_provider: my.project.utils.CustomIdentifierGeneratorStrategyRegistrationProvider

This setup leverages the new GenerationTypeStrategyRegistrationProvider to register and use your custom sequence generator with Hibernate 6.x.