HHH-18139 start trying to simplify things
Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
parent
cda6e58427
commit
7f3b777cd1
|
@ -362,8 +362,8 @@ public class GeneratorBinder {
|
||||||
final Class<? extends Annotation> annotationType = annotation.getAnnotationType();
|
final Class<? extends Annotation> annotationType = annotation.getAnnotationType();
|
||||||
final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class );
|
final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class );
|
||||||
assert idGeneratorType != null;
|
assert idGeneratorType != null;
|
||||||
return creationContext -> {
|
|
||||||
final Class<? extends Generator> generatorClass = idGeneratorType.value();
|
final Class<? extends Generator> generatorClass = idGeneratorType.value();
|
||||||
|
return creationContext -> {
|
||||||
checkGeneratorClass( generatorClass );
|
checkGeneratorClass( generatorClass );
|
||||||
final Generator generator = instantiateGenerator(
|
final Generator generator = instantiateGenerator(
|
||||||
annotation,
|
annotation,
|
||||||
|
|
|
@ -6,39 +6,110 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.id.factory.internal;
|
package org.hibernate.id.factory.internal;
|
||||||
|
|
||||||
|
import org.hibernate.InstantiationException;
|
||||||
|
import org.hibernate.MappingException;
|
||||||
|
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.engine.config.spi.ConfigurationService;
|
import org.hibernate.engine.config.spi.ConfigurationService;
|
||||||
import org.hibernate.engine.config.spi.StandardConverters;
|
import org.hibernate.engine.config.spi.StandardConverters;
|
||||||
|
import org.hibernate.id.Assigned;
|
||||||
|
import org.hibernate.id.Configurable;
|
||||||
|
import org.hibernate.id.ForeignGenerator;
|
||||||
|
import org.hibernate.id.GUIDGenerator;
|
||||||
import org.hibernate.id.IdentifierGenerator;
|
import org.hibernate.id.IdentifierGenerator;
|
||||||
|
import org.hibernate.id.IdentityGenerator;
|
||||||
|
import org.hibernate.id.IncrementGenerator;
|
||||||
import org.hibernate.id.OptimizableGenerator;
|
import org.hibernate.id.OptimizableGenerator;
|
||||||
import org.hibernate.id.PersistentIdentifierGenerator;
|
import org.hibernate.id.PersistentIdentifierGenerator;
|
||||||
|
import org.hibernate.id.SelectGenerator;
|
||||||
|
import org.hibernate.id.UUIDGenerator;
|
||||||
|
import org.hibernate.id.UUIDHexGenerator;
|
||||||
import org.hibernate.id.enhanced.LegacyNamingStrategy;
|
import org.hibernate.id.enhanced.LegacyNamingStrategy;
|
||||||
|
import org.hibernate.id.enhanced.SequenceStyleGenerator;
|
||||||
import org.hibernate.id.enhanced.SingleNamingStrategy;
|
import org.hibernate.id.enhanced.SingleNamingStrategy;
|
||||||
import org.hibernate.id.factory.IdentifierGeneratorFactory;
|
import org.hibernate.id.enhanced.TableGenerator;
|
||||||
import org.hibernate.mapping.Column;
|
import org.hibernate.mapping.Column;
|
||||||
import org.hibernate.mapping.RootClass;
|
import org.hibernate.mapping.RootClass;
|
||||||
import org.hibernate.mapping.SimpleValue;
|
import org.hibernate.mapping.SimpleValue;
|
||||||
import org.hibernate.mapping.Table;
|
import org.hibernate.mapping.Table;
|
||||||
import org.hibernate.generator.Generator;
|
import org.hibernate.generator.Generator;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor;
|
||||||
|
|
||||||
public class IdentifierGeneratorUtil {
|
public class IdentifierGeneratorUtil {
|
||||||
|
|
||||||
public static Generator createLegacyIdentifierGenerator(
|
public static Generator createLegacyIdentifierGenerator(
|
||||||
SimpleValue simpleValue,
|
SimpleValue simpleValue,
|
||||||
IdentifierGeneratorFactory identifierGeneratorFactory,
|
|
||||||
Dialect dialect,
|
Dialect dialect,
|
||||||
String defaultCatalog,
|
String defaultCatalog,
|
||||||
String defaultSchema,
|
String defaultSchema,
|
||||||
RootClass rootClass) {
|
RootClass rootClass) {
|
||||||
return identifierGeneratorFactory.createIdentifierGenerator(
|
final Class<? extends Generator> generatorClass = generatorClass( simpleValue );
|
||||||
simpleValue.getIdentifierGeneratorStrategy(),
|
final Constructor<? extends Generator> defaultConstructor = getDefaultConstructor( generatorClass );
|
||||||
simpleValue.getType(),
|
if ( defaultConstructor == null ) {
|
||||||
collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass )
|
throw new InstantiationException( "No default constructor for id generator class", generatorClass );
|
||||||
);
|
}
|
||||||
|
final Generator identifierGenerator;
|
||||||
|
try {
|
||||||
|
identifierGenerator = defaultConstructor.newInstance();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new InstantiationException( "Could not instantiate id generator", generatorClass, e );
|
||||||
|
}
|
||||||
|
if ( identifierGenerator instanceof Configurable ) {
|
||||||
|
final Properties parameters = collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass );
|
||||||
|
final Configurable configurable = (Configurable) identifierGenerator;
|
||||||
|
configurable.configure( simpleValue.getType(), parameters, simpleValue.getServiceRegistry() );
|
||||||
|
}
|
||||||
|
return identifierGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class<? extends Generator> generatorClass(SimpleValue simpleValue) {
|
||||||
|
String strategy = simpleValue.getIdentifierGeneratorStrategy();
|
||||||
|
if ( "native".equals(strategy) ) {
|
||||||
|
strategy =
|
||||||
|
simpleValue.getMetadata().getDatabase().getDialect()
|
||||||
|
.getNativeIdentifierGeneratorStrategy();
|
||||||
|
}
|
||||||
|
switch (strategy) {
|
||||||
|
case "assigned":
|
||||||
|
return Assigned.class;
|
||||||
|
case "enhanced-sequence":
|
||||||
|
case "sequence":
|
||||||
|
return SequenceStyleGenerator.class;
|
||||||
|
case "enhanced-table":
|
||||||
|
case "table":
|
||||||
|
return TableGenerator.class;
|
||||||
|
case "identity":
|
||||||
|
return IdentityGenerator.class;
|
||||||
|
case "increment":
|
||||||
|
return IncrementGenerator.class;
|
||||||
|
case "foreign":
|
||||||
|
return ForeignGenerator.class;
|
||||||
|
case "uuid":
|
||||||
|
case "uuid.hex":
|
||||||
|
return UUIDHexGenerator.class;
|
||||||
|
case "uuid2":
|
||||||
|
return UUIDGenerator.class;
|
||||||
|
case "select":
|
||||||
|
return SelectGenerator.class;
|
||||||
|
case "guid":
|
||||||
|
return GUIDGenerator.class;
|
||||||
|
}
|
||||||
|
final Class<? extends Generator> clazz =
|
||||||
|
simpleValue.getServiceRegistry().requireService( ClassLoaderService.class )
|
||||||
|
.classForName( strategy );
|
||||||
|
if ( !Generator.class.isAssignableFrom( clazz ) ) {
|
||||||
|
// in principle, this shouldn't happen, since @GenericGenerator
|
||||||
|
// constrains the type to subtypes of Generator
|
||||||
|
throw new MappingException( clazz.getName() + " does not implement 'Generator'" );
|
||||||
|
}
|
||||||
|
return clazz;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Properties collectParameters(
|
public static Properties collectParameters(
|
||||||
|
|
|
@ -402,7 +402,7 @@ public abstract class SimpleValue implements KeyValue {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
generator = createLegacyIdentifierGenerator(this, identifierGeneratorFactory, dialect, null, null, rootClass );
|
generator = createLegacyIdentifierGenerator(this, dialect, null, null, rootClass );
|
||||||
if ( generator instanceof IdentityGenerator ) {
|
if ( generator instanceof IdentityGenerator ) {
|
||||||
setColumnToIdentity();
|
setColumnToIdentity();
|
||||||
}
|
}
|
||||||
|
@ -872,7 +872,7 @@ public abstract class SimpleValue implements KeyValue {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isColumnInsertable(int index) {
|
public boolean isColumnInsertable(int index) {
|
||||||
if ( insertability.size() > 0 ) {
|
if ( !insertability.isEmpty() ) {
|
||||||
return insertability.get( index );
|
return insertability.get( index );
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -880,7 +880,7 @@ public abstract class SimpleValue implements KeyValue {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isColumnUpdateable(int index) {
|
public boolean isColumnUpdateable(int index) {
|
||||||
if ( updatability.size() > 0 ) {
|
if ( !updatability.isEmpty() ) {
|
||||||
return updatability.get( index );
|
return updatability.get( index );
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in New Issue