Search in sources :

Example 1 with SequenceGenerator

use of jakarta.persistence.SequenceGenerator in project hibernate-orm by hibernate.

the class AnnotationBinder method bindPackage.

public static void bindPackage(ClassLoaderService cls, String packageName, MetadataBuildingContext context) {
    final Package packaze = cls.packageForNameOrNull(packageName);
    if (packaze == null) {
        return;
    }
    final XPackage pckg = context.getBootstrapContext().getReflectionManager().toXPackage(packaze);
    if (pckg.isAnnotationPresent(SequenceGenerator.class)) {
        SequenceGenerator ann = pckg.getAnnotation(SequenceGenerator.class);
        IdentifierGeneratorDefinition idGen = buildIdGenerator(ann, context);
        context.getMetadataCollector().addIdentifierGenerator(idGen);
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Add sequence generator with name: {0}", idGen.getName());
        }
    }
    if (pckg.isAnnotationPresent(SequenceGenerators.class)) {
        SequenceGenerators ann = pckg.getAnnotation(SequenceGenerators.class);
        for (SequenceGenerator tableGenerator : ann.value()) {
            context.getMetadataCollector().addIdentifierGenerator(buildIdGenerator(tableGenerator, context));
        }
    }
    if (pckg.isAnnotationPresent(TableGenerator.class)) {
        TableGenerator ann = pckg.getAnnotation(TableGenerator.class);
        IdentifierGeneratorDefinition idGen = buildIdGenerator(ann, context);
        context.getMetadataCollector().addIdentifierGenerator(idGen);
    }
    if (pckg.isAnnotationPresent(TableGenerators.class)) {
        TableGenerators ann = pckg.getAnnotation(TableGenerators.class);
        for (TableGenerator tableGenerator : ann.value()) {
            context.getMetadataCollector().addIdentifierGenerator(buildIdGenerator(tableGenerator, context));
        }
    }
    handleTypeDescriptorRegistrations(pckg, context);
    bindEmbeddableInstantiatorRegistrations(pckg, context);
    bindCompositeUserTypeRegistrations(pckg, context);
    bindGenericGenerators(pckg, context);
    bindQueries(pckg, context);
    bindFilterDefs(pckg, context);
}
Also used : SequenceGenerators(jakarta.persistence.SequenceGenerators) SequenceGenerator(jakarta.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) TableGenerators(jakarta.persistence.TableGenerators) XPackage(org.hibernate.annotations.common.reflection.XPackage) XPackage(org.hibernate.annotations.common.reflection.XPackage) TableGenerator(jakarta.persistence.TableGenerator)

Example 2 with SequenceGenerator

use of jakarta.persistence.SequenceGenerator in project hibernate-orm by hibernate.

the class AnnotationBinder method buildGenerators.

private static HashMap<String, IdentifierGeneratorDefinition> buildGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) {
    InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
    HashMap<String, IdentifierGeneratorDefinition> generators = new HashMap<>();
    TableGenerators tableGenerators = annElt.getAnnotation(TableGenerators.class);
    if (tableGenerators != null) {
        for (TableGenerator tableGenerator : tableGenerators.value()) {
            IdentifierGeneratorDefinition idGenerator = buildIdGenerator(tableGenerator, context);
            generators.put(idGenerator.getName(), idGenerator);
            metadataCollector.addIdentifierGenerator(idGenerator);
        }
    }
    SequenceGenerators sequenceGenerators = annElt.getAnnotation(SequenceGenerators.class);
    if (sequenceGenerators != null) {
        for (SequenceGenerator sequenceGenerator : sequenceGenerators.value()) {
            IdentifierGeneratorDefinition idGenerator = buildIdGenerator(sequenceGenerator, context);
            generators.put(idGenerator.getName(), idGenerator);
            metadataCollector.addIdentifierGenerator(idGenerator);
        }
    }
    TableGenerator tabGen = annElt.getAnnotation(TableGenerator.class);
    SequenceGenerator seqGen = annElt.getAnnotation(SequenceGenerator.class);
    GenericGenerator genGen = annElt.getAnnotation(GenericGenerator.class);
    if (tabGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(tabGen, context);
        generators.put(idGen.getName(), idGen);
        metadataCollector.addIdentifierGenerator(idGen);
    }
    if (seqGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(seqGen, context);
        generators.put(idGen.getName(), idGen);
        metadataCollector.addIdentifierGenerator(idGen);
    }
    if (genGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(genGen, context);
        generators.put(idGen.getName(), idGen);
        metadataCollector.addIdentifierGenerator(idGen);
    }
    return generators;
}
Also used : SequenceGenerators(jakarta.persistence.SequenceGenerators) InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) HashMap(java.util.HashMap) SequenceGenerator(jakarta.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) TableGenerators(jakarta.persistence.TableGenerators) TableGenerator(jakarta.persistence.TableGenerator) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 3 with SequenceGenerator

use of jakarta.persistence.SequenceGenerator in project hibernate-orm by hibernate.

the class AnnotationBinder method buildIdGenerator.

private static IdentifierGeneratorDefinition buildIdGenerator(java.lang.annotation.Annotation generatorAnn, MetadataBuildingContext context) {
    if (generatorAnn == null) {
        return null;
    }
    IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder();
    if (generatorAnn instanceof TableGenerator) {
        context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator((TableGenerator) generatorAnn, definitionBuilder);
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Add table generator with name: {0}", definitionBuilder.getName());
        }
    } else if (generatorAnn instanceof SequenceGenerator) {
        context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretSequenceGenerator((SequenceGenerator) generatorAnn, definitionBuilder);
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Add sequence generator with name: {0}", definitionBuilder.getName());
        }
    } else if (generatorAnn instanceof GenericGenerator) {
        GenericGenerator genGen = (GenericGenerator) generatorAnn;
        definitionBuilder.setName(genGen.name());
        definitionBuilder.setStrategy(genGen.strategy());
        Parameter[] params = genGen.parameters();
        for (Parameter parameter : params) {
            definitionBuilder.addParam(parameter.name(), parameter.value());
        }
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Add generic generator with name: {0}", definitionBuilder.getName());
        }
    } else {
        throw new AssertionFailure("Unknown Generator annotation: " + generatorAnn);
    }
    return definitionBuilder.build();
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SequenceGenerator(jakarta.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) Parameter(org.hibernate.annotations.Parameter) TableGenerator(jakarta.persistence.TableGenerator) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 4 with SequenceGenerator

use of jakarta.persistence.SequenceGenerator 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)

Example 5 with SequenceGenerator

use of jakarta.persistence.SequenceGenerator in project hibernate-orm by hibernate.

the class JPAXMLOverriddenMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (!xmlMappingEnabled) {
        return Collections.emptyMap();
    } else {
        if (defaults == null) {
            defaults = new HashMap<>();
            XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema();
            defaults.put("schema", xmlDefaults.getSchema());
            defaults.put("catalog", xmlDefaults.getCatalog());
            defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
            defaults.put("cascade-persist", xmlDefaults.getCascadePersist());
            List<Class> entityListeners = new ArrayList<Class>();
            for (String className : xmlContext.getDefaultEntityListeners()) {
                try {
                    entityListeners.add(classLoaderAccess.classForName(className));
                } catch (ClassLoadingException e) {
                    throw new IllegalStateException("Default entity listener class not found: " + className);
                }
            }
            defaults.put(EntityListeners.class, entityListeners);
            for (JaxbEntityMappings entityMappings : xmlContext.getAllDocuments()) {
                List<JaxbSequenceGenerator> jaxbSequenceGenerators = entityMappings.getSequenceGenerator();
                List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
                if (sequenceGenerators == null) {
                    sequenceGenerators = new ArrayList<>();
                    defaults.put(SequenceGenerator.class, sequenceGenerators);
                }
                for (JaxbSequenceGenerator element : jaxbSequenceGenerators) {
                    sequenceGenerators.add(JPAXMLOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(element));
                }
                List<JaxbTableGenerator> jaxbTableGenerators = entityMappings.getTableGenerator();
                List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
                if (tableGenerators == null) {
                    tableGenerators = new ArrayList<>();
                    defaults.put(TableGenerator.class, tableGenerators);
                }
                for (JaxbTableGenerator element : jaxbTableGenerators) {
                    tableGenerators.add(JPAXMLOverriddenAnnotationReader.buildTableGeneratorAnnotation(element, xmlDefaults));
                }
                List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
                if (namedQueries == null) {
                    namedQueries = new ArrayList<>();
                    defaults.put(NamedQuery.class, namedQueries);
                }
                List<NamedQuery> currentNamedQueries = JPAXMLOverriddenAnnotationReader.buildNamedQueries(entityMappings.getNamedQuery(), xmlDefaults, classLoaderAccess);
                namedQueries.addAll(currentNamedQueries);
                List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
                if (namedNativeQueries == null) {
                    namedNativeQueries = new ArrayList<>();
                    defaults.put(NamedNativeQuery.class, namedNativeQueries);
                }
                List<NamedNativeQuery> currentNamedNativeQueries = JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(entityMappings.getNamedNativeQuery(), xmlDefaults, classLoaderAccess);
                namedNativeQueries.addAll(currentNamedNativeQueries);
                List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
                if (sqlResultSetMappings == null) {
                    sqlResultSetMappings = new ArrayList<>();
                    defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
                }
                List<SqlResultSetMapping> currentSqlResultSetMappings = JPAXMLOverriddenAnnotationReader.buildSqlResultsetMappings(entityMappings.getSqlResultSetMapping(), xmlDefaults, classLoaderAccess);
                sqlResultSetMappings.addAll(currentSqlResultSetMappings);
                List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
                if (namedStoredProcedureQueries == null) {
                    namedStoredProcedureQueries = new ArrayList<>();
                    defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
                }
                List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAXMLOverriddenAnnotationReader.buildNamedStoreProcedureQueries(entityMappings.getNamedStoredProcedureQuery(), xmlDefaults, classLoaderAccess);
                namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
            }
        }
        return defaults;
    }
}
Also used : JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGenerator) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) ArrayList(java.util.ArrayList) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbTableGenerator) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGenerator) SequenceGenerator(jakarta.persistence.SequenceGenerator) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbTableGenerator) TableGenerator(jakarta.persistence.TableGenerator) NamedQuery(jakarta.persistence.NamedQuery) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(jakarta.persistence.NamedStoredProcedureQuery)

Aggregations

SequenceGenerator (jakarta.persistence.SequenceGenerator)5 TableGenerator (jakarta.persistence.TableGenerator)5 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)4 SequenceGenerators (jakarta.persistence.SequenceGenerators)2 TableGenerators (jakarta.persistence.TableGenerators)2 GenericGenerator (org.hibernate.annotations.GenericGenerator)2 GeneratedValue (jakarta.persistence.GeneratedValue)1 GenerationType (jakarta.persistence.GenerationType)1 Index (jakarta.persistence.Index)1 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)1 NamedQuery (jakarta.persistence.NamedQuery)1 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)1 SqlResultSetMapping (jakarta.persistence.SqlResultSetMapping)1 UniqueConstraint (jakarta.persistence.UniqueConstraint)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AssertionFailure (org.hibernate.AssertionFailure)1 Parameter (org.hibernate.annotations.Parameter)1