Skip to content

Commit

Permalink
HHH-14755 Allow configuring the DefaultIdentifierGeneratorFactory to …
Browse files Browse the repository at this point in the history
…ignore BeanContainer(s)
  • Loading branch information
Sanne committed Jul 29, 2021
1 parent d4ed421 commit 83975ea
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,30 @@ public class DefaultIdentifierGeneratorFactory

private static final CoreMessageLogger LOG = CoreLogging.messageLogger( DefaultIdentifierGeneratorFactory.class );

private final boolean ignoreBeanContainer;

private ServiceRegistry serviceRegistry;
private Dialect dialect;

private ConcurrentHashMap<String, Class> generatorStrategyToClassNameMap = new ConcurrentHashMap<String, Class>();
private final ConcurrentHashMap<String, Class> generatorStrategyToClassNameMap = new ConcurrentHashMap<>();

private BeanContainer beanContainer;

/**
* Constructs a new DefaultIdentifierGeneratorFactory.
*/
public DefaultIdentifierGeneratorFactory() {
this( false );
}

/**
* Allows to explicitly control if the BeanContainer should be ignored
* (if there is one registered) when initializing any new IdentifierGenerator
* instances.
* @param ignoreBeanContainer
*/
public DefaultIdentifierGeneratorFactory(boolean ignoreBeanContainer) {
this.ignoreBeanContainer = ignoreBeanContainer;
register( "uuid2", UUIDGenerator.class );
register( "guid", GUIDGenerator.class ); // can be done with UUIDGenerator + strategy
register( "uuid", UUIDHexGenerator.class ); // "deprecated" for new use
Expand Down Expand Up @@ -102,9 +117,8 @@ public void setDialect(Dialect dialect) {
public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config) {
try {
Class clazz = getIdentifierGeneratorClass( strategy );
BeanContainer beanContainer = serviceRegistry.getService(ManagedBeanRegistry.class).getBeanContainer();
IdentifierGenerator identifierGenerator;
if ( generatorStrategyToClassNameMap.containsKey(strategy) || beanContainer == null ) {
if ( beanContainer == null || generatorStrategyToClassNameMap.containsKey( strategy ) ) {
identifierGenerator = ( IdentifierGenerator ) clazz.newInstance();
}
else {
Expand Down Expand Up @@ -163,14 +177,18 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
this.dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
if ( ! this.ignoreBeanContainer ) {
this.beanContainer = serviceRegistry.getService( ManagedBeanRegistry.class ).getBeanContainer();
//else we just have beanContainer = null;
}

final boolean useNewIdentifierGenerators = configService.getSetting(
AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
StandardConverters.BOOLEAN,
true
);

if(!useNewIdentifierGenerators) {
if ( ! useNewIdentifierGenerators ) {
register( "sequence", SequenceGenerator.class );
}
}
Expand Down

0 comments on commit 83975ea

Please sign in to comment.