HHH-18139 some cleanups to GeneratorBinder
Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
parent
63ee06685c
commit
3946cfaf0b
|
@ -68,6 +68,8 @@ import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||||
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
||||||
|
|
||||||
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
import org.hibernate.type.Type;
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
@ -83,40 +85,55 @@ import static org.hibernate.boot.model.internal.BinderHelper.isGlobalGeneratorNa
|
||||||
import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor;
|
import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor;
|
||||||
import static org.hibernate.mapping.SimpleValue.DEFAULT_ID_GEN_STRATEGY;
|
import static org.hibernate.mapping.SimpleValue.DEFAULT_ID_GEN_STRATEGY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responsible for configuring and instantiating {@link Generator}s.
|
||||||
|
*
|
||||||
|
* @author Gavin King
|
||||||
|
*/
|
||||||
public class GeneratorBinder {
|
public class GeneratorBinder {
|
||||||
|
|
||||||
private static final Logger LOG = CoreLogging.logger( BinderHelper.class );
|
private static final Logger LOG = CoreLogging.logger( BinderHelper.class );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an id generator for the given named {@code strategy} using the
|
||||||
|
* "old" (pre-Hibernate 6) approach.
|
||||||
|
*/
|
||||||
public static Generator createLegacyIdentifierGenerator(
|
public static Generator createLegacyIdentifierGenerator(
|
||||||
String strategy,
|
String strategy,
|
||||||
SimpleValue simpleValue,
|
SimpleValue idValue,
|
||||||
Dialect dialect,
|
Dialect dialect,
|
||||||
RootClass rootClass,
|
RootClass rootClass,
|
||||||
Map<String, Object> configuration) {
|
Map<String, Object> configuration) {
|
||||||
final Class<? extends Generator> generatorClass = generatorClass( strategy, simpleValue );
|
final Generator identifierGenerator = instantiateGenerator( generatorClass( strategy, idValue ) );
|
||||||
final Constructor<? extends Generator> defaultConstructor = getDefaultConstructor( generatorClass );
|
|
||||||
if ( defaultConstructor == null ) {
|
|
||||||
throw new org.hibernate.InstantiationException( "No default constructor for id generator class", generatorClass );
|
|
||||||
}
|
|
||||||
final Generator identifierGenerator;
|
|
||||||
try {
|
|
||||||
identifierGenerator = defaultConstructor.newInstance();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
throw new org.hibernate.InstantiationException( "Could not instantiate id generator", generatorClass, e );
|
|
||||||
}
|
|
||||||
if ( identifierGenerator instanceof Configurable ) {
|
if ( identifierGenerator instanceof Configurable ) {
|
||||||
final Properties parameters = collectParameters( simpleValue, dialect, rootClass, configuration );
|
|
||||||
final Configurable configurable = (Configurable) identifierGenerator;
|
final Configurable configurable = (Configurable) identifierGenerator;
|
||||||
configurable.configure( simpleValue.getType(), parameters, simpleValue.getServiceRegistry() );
|
configurable.configure( idValue.getType(),
|
||||||
|
collectParameters( idValue, dialect, rootClass, configuration ),
|
||||||
|
idValue.getServiceRegistry() );
|
||||||
}
|
}
|
||||||
return identifierGenerator;
|
return identifierGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Class<? extends Generator> generatorClass(String strategy, SimpleValue simpleValue) {
|
private static Generator instantiateGenerator(Class<? extends Generator> generatorClass) {
|
||||||
|
final Constructor<? extends Generator> defaultConstructor = getDefaultConstructor( generatorClass );
|
||||||
|
if ( defaultConstructor == null ) {
|
||||||
|
throw new org.hibernate.InstantiationException( "No default constructor for id generator class", generatorClass);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return defaultConstructor.newInstance();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new org.hibernate.InstantiationException( "Could not instantiate id generator", generatorClass, e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interpret an "old" generator strategy name as a {@link Generator} class.
|
||||||
|
*/
|
||||||
|
private static Class<? extends Generator> generatorClass(String strategy, SimpleValue idValue) {
|
||||||
if ( "native".equals(strategy) ) {
|
if ( "native".equals(strategy) ) {
|
||||||
strategy =
|
strategy =
|
||||||
simpleValue.getMetadata().getDatabase().getDialect()
|
idValue.getMetadata().getDatabase().getDialect()
|
||||||
.getNativeIdentifierGeneratorStrategy();
|
.getNativeIdentifierGeneratorStrategy();
|
||||||
}
|
}
|
||||||
switch (strategy) {
|
switch (strategy) {
|
||||||
|
@ -145,7 +162,7 @@ public class GeneratorBinder {
|
||||||
return GUIDGenerator.class;
|
return GUIDGenerator.class;
|
||||||
}
|
}
|
||||||
final Class<? extends Generator> clazz =
|
final Class<? extends Generator> clazz =
|
||||||
simpleValue.getServiceRegistry().requireService( ClassLoaderService.class )
|
idValue.getServiceRegistry().requireService( ClassLoaderService.class )
|
||||||
.classForName( strategy );
|
.classForName( strategy );
|
||||||
if ( !Generator.class.isAssignableFrom( clazz ) ) {
|
if ( !Generator.class.isAssignableFrom( clazz ) ) {
|
||||||
// in principle, this shouldn't happen, since @GenericGenerator
|
// in principle, this shouldn't happen, since @GenericGenerator
|
||||||
|
@ -155,6 +172,10 @@ public class GeneratorBinder {
|
||||||
return clazz;
|
return clazz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect the parameters which should be passed to
|
||||||
|
* {@link Configurable#configure(Type, Properties, ServiceRegistry)}.
|
||||||
|
*/
|
||||||
public static Properties collectParameters(
|
public static Properties collectParameters(
|
||||||
SimpleValue simpleValue,
|
SimpleValue simpleValue,
|
||||||
Dialect dialect,
|
Dialect dialect,
|
||||||
|
@ -279,16 +300,12 @@ public class GeneratorBinder {
|
||||||
+ " (define a named generator using '@SequenceGenerator', '@TableGenerator', or '@GenericGenerator')" );
|
+ " (define a named generator using '@SequenceGenerator', '@TableGenerator', or '@GenericGenerator')" );
|
||||||
}
|
}
|
||||||
//This is quite vague in the spec but a generator could override the generator choice
|
//This is quite vague in the spec but a generator could override the generator choice
|
||||||
|
generatorStrategy =
|
||||||
|
generatorType == null
|
||||||
//yuk! this is a hack not to override 'AUTO' even if generator is set
|
//yuk! this is a hack not to override 'AUTO' even if generator is set
|
||||||
final boolean avoidOverriding =
|
|| !definition.getStrategy().equals( "identity" )
|
||||||
definition.getStrategy().equals( "identity" )
|
? definition.getStrategy()
|
||||||
|| definition.getStrategy().equals( "seqhilo" );
|
: generatorType;
|
||||||
if ( generatorType == null || !avoidOverriding ) {
|
|
||||||
generatorStrategy = definition.getStrategy();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
generatorStrategy = generatorType;
|
|
||||||
}
|
|
||||||
//checkIfMatchingGenerator(definition, generatorType, generatorName);
|
//checkIfMatchingGenerator(definition, generatorType, generatorName);
|
||||||
parameters.putAll( definition.getParameters() );
|
parameters.putAll( definition.getParameters() );
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,7 @@ public class Furniture {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(generator = "hibseq")
|
@GeneratedValue(generator = "hibseq")
|
||||||
@GenericGenerator(name = "hibseq", strategy = "seqhilo",
|
@GenericGenerator(name = "hibseq", strategy = "increment")
|
||||||
parameters = {
|
|
||||||
@Parameter(name = "max_lo", value = "5"),
|
|
||||||
@Parameter(name = "sequence", value = "heybabyhey")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,7 @@ public class Furniture {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(generator = "hibseq")
|
@GeneratedValue(generator = "hibseq")
|
||||||
@GenericGenerator(name = "hibseq", strategy = "seqhilo",
|
@GenericGenerator(name = "hibseq", strategy = "increment")
|
||||||
parameters = {
|
|
||||||
@Parameter(name = "max_lo", value = "5"),
|
|
||||||
@Parameter(name = "sequence", value = "heybabyhey")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue