Search in sources :

Example 1 with GeneratorNameDeterminationContext

use of org.hibernate.boot.model.IdGeneratorStrategyInterpreter.GeneratorNameDeterminationContext in project hibernate-orm by hibernate.

the class BinderHelper method getIdentifierGenerator.

private static IdentifierGeneratorDefinition getIdentifierGenerator(String name, XProperty idXProperty, Map<String, IdentifierGeneratorDefinition> localGenerators, MetadataBuildingContext buildingContext) {
    if (localGenerators != null) {
        final IdentifierGeneratorDefinition result = localGenerators.get(name);
        if (result != null) {
            return result;
        }
    }
    final IdentifierGeneratorDefinition globalDefinition = buildingContext.getMetadataCollector().getIdentifierGenerator(name);
    if (globalDefinition != null) {
        return globalDefinition;
    }
    log.debugf("Could not resolve explicit IdentifierGeneratorDefinition - using implicit interpretation (%s)", name);
    // If we were unable to locate an actual matching named generator assume a sequence/table of the given name.
    // this really needs access to the `jakarta.persistence.GenerationType` to work completely properly
    // 
    // (the crux of HHH-12122)
    // temporarily, in lieu of having access to GenerationType, assume the EnhancedSequenceGenerator
    // for the purpose of testing the feasibility of the approach
    final GeneratedValue generatedValueAnn = idXProperty.getAnnotation(GeneratedValue.class);
    if (generatedValueAnn == null) {
        // this should really never happen, but its easy to protect against it...
        return new IdentifierGeneratorDefinition("assigned", "assigned");
    }
    final IdGeneratorStrategyInterpreter generationInterpreter = buildingContext.getBuildingOptions().getIdGenerationTypeInterpreter();
    final GenerationType generationType = interpretGenerationType(generatedValueAnn);
    if (generationType == null || generationType == GenerationType.SEQUENCE) {
        // NOTE : `null` will ultimately be interpreted as "hibernate_sequence"
        log.debugf("Building implicit sequence-based IdentifierGeneratorDefinition (%s)", name);
        final IdentifierGeneratorDefinition.Builder builder = new IdentifierGeneratorDefinition.Builder();
        generationInterpreter.interpretSequenceGenerator(new SequenceGenerator() {

            @Override
            public String name() {
                return name;
            }

            @Override
            public String sequenceName() {
                return "";
            }

            @Override
            public String catalog() {
                return "";
            }

            @Override
            public String schema() {
                return "";
            }

            @Override
            public int initialValue() {
                return 1;
            }

            @Override
            public int allocationSize() {
                return 50;
            }

            @Override
            public Class<? extends Annotation> annotationType() {
                return SequenceGenerator.class;
            }
        }, builder);
        return builder.build();
    } else if (generationType == GenerationType.TABLE) {
        // NOTE : `null` will ultimately be interpreted as "hibernate_sequence"
        log.debugf("Building implicit table-based IdentifierGeneratorDefinition (%s)", name);
        final IdentifierGeneratorDefinition.Builder builder = new IdentifierGeneratorDefinition.Builder();
        generationInterpreter.interpretTableGenerator(new TableGenerator() {

            @Override
            public String name() {
                return name;
            }

            @Override
            public String table() {
                return "";
            }

            @Override
            public int initialValue() {
                return 0;
            }

            @Override
            public int allocationSize() {
                return 50;
            }

            @Override
            public String catalog() {
                return "";
            }

            @Override
            public String schema() {
                return "";
            }

            @Override
            public String pkColumnName() {
                return "";
            }

            @Override
            public String valueColumnName() {
                return "";
            }

            @Override
            public String pkColumnValue() {
                return "";
            }

            @Override
            public UniqueConstraint[] uniqueConstraints() {
                return new UniqueConstraint[0];
            }

            @Override
            public Index[] indexes() {
                return new Index[0];
            }

            @Override
            public Class<? extends Annotation> annotationType() {
                return TableGenerator.class;
            }
        }, builder);
        return builder.build();
    }
    // really AUTO and IDENTITY work the same in this respect, aside from the actual strategy name
    final String strategyName;
    if (generationType == GenerationType.IDENTITY) {
        strategyName = "identity";
    } else {
        strategyName = generationInterpreter.determineGeneratorName(generationType, new GeneratorNameDeterminationContext() {

            @Override
            public Class<?> getIdType() {
                return buildingContext.getBootstrapContext().getReflectionManager().toClass(idXProperty.getType());
            }

            @Override
            public String getGeneratedValueGeneratorName() {
                return generatedValueAnn.generator();
            }
        });
    }
    log.debugf("Building implicit generic IdentifierGeneratorDefinition (%s) : %s", name, strategyName);
    return new IdentifierGeneratorDefinition(name, strategyName, Collections.singletonMap(IdentifierGenerator.GENERATOR_NAME, name));
}
Also used : SequenceGenerator(jakarta.persistence.SequenceGenerator) IdGeneratorStrategyInterpreter(org.hibernate.boot.model.IdGeneratorStrategyInterpreter) UniqueConstraint(jakarta.persistence.UniqueConstraint) Index(jakarta.persistence.Index) TableGenerator(jakarta.persistence.TableGenerator) AnnotatedColumn.buildColumnOrFormulaFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnOrFormulaFromAnnotation) Annotation(java.lang.annotation.Annotation) GenerationType(jakarta.persistence.GenerationType) GeneratedValue(jakarta.persistence.GeneratedValue) GeneratorNameDeterminationContext(org.hibernate.boot.model.IdGeneratorStrategyInterpreter.GeneratorNameDeterminationContext) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass)

Aggregations

GeneratedValue (jakarta.persistence.GeneratedValue)1 GenerationType (jakarta.persistence.GenerationType)1 Index (jakarta.persistence.Index)1 SequenceGenerator (jakarta.persistence.SequenceGenerator)1 TableGenerator (jakarta.persistence.TableGenerator)1 UniqueConstraint (jakarta.persistence.UniqueConstraint)1 Annotation (java.lang.annotation.Annotation)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 IdGeneratorStrategyInterpreter (org.hibernate.boot.model.IdGeneratorStrategyInterpreter)1 GeneratorNameDeterminationContext (org.hibernate.boot.model.IdGeneratorStrategyInterpreter.GeneratorNameDeterminationContext)1 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)1 AnnotatedColumn.buildColumnOrFormulaFromAnnotation (org.hibernate.cfg.AnnotatedColumn.buildColumnOrFormulaFromAnnotation)1 PersistentClass (org.hibernate.mapping.PersistentClass)1