Class SnowflakeIdGeneratorHolder

java.lang.Object
com.fayupable.snowflake.jpa.SnowflakeIdGeneratorHolder

public final class SnowflakeIdGeneratorHolder extends Object
Static bridge between a Spring/DI-managed IdGenerator and Hibernate, which instantiates SnowflakeIdentifierGenerator via reflection and never consults the dependency injection container. Callers not using Spring must invoke setInstance(IdGenerator) manually before the first entity is persisted.
  • Method Details

    • setInstance

      public static void setInstance(IdGenerator generator)
      Registers the IdGenerator that getInstance() will return. Safe to call more than once; the latest call always wins, which allows a Spring @Bean method to re-register the generator whenever a new application context is created (e.g. across independent test contexts in the same JVM).
      Parameters:
      generator - the generator to publish; typically a singleton configured with the application's nodeId and epoch
    • getInstance

      public static IdGenerator getInstance()
      Returns:
      the generator previously passed to setInstance(IdGenerator)
      Throws:
      IllegalStateException - if no generator has been registered yet, meaning either the Spring auto-configuration was never loaded or a non-Spring caller forgot to call setInstance(IdGenerator) before persisting
    • reset

      public static void reset()
      Clears the held instance so it does not leak across independent test contexts. Not intended for production use. Call this from test teardown only, typically once per test class rather than per test method, since Spring reuses a cached ApplicationContext (and therefore the same registered generator) across test methods within that class.