Package com.fayupable.snowflake.jpa
Class SnowflakeIdGeneratorHolder
java.lang.Object
com.fayupable.snowflake.jpa.SnowflakeIdGeneratorHolder
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 Summary
Modifier and TypeMethodDescriptionstatic IdGeneratorReturns the currently registeredIdGenerator.static voidreset()Clears the held instance so it does not leak across independent test contexts.static voidsetInstance(IdGenerator generator) Registers theIdGeneratorthatgetInstance()will return.
-
Method Details
-
setInstance
Registers theIdGeneratorthatgetInstance()will return. Safe to call more than once; the latest call always wins, which allows a Spring@Beanmethod 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'snodeIdandepoch
-
getInstance
Returns the currently registeredIdGenerator. Called bySnowflakeIdentifierGenerator.generate(org.hibernate.engine.spi.SharedSessionContractImplementor, java.lang.Object)on every entity insert.- 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 callsetInstance(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 cachedApplicationContext(and therefore the same registered generator) across test methods within that class.
-