Search in sources :

Example 1 with SequenceGenerator

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

the class AnnotationBinder method bindPackage.

public static void bindPackage(String packageName, MetadataBuildingContext context) {
    XPackage pckg;
    try {
        pckg = context.getBootstrapContext().getReflectionManager().packageForName(packageName);
    } catch (ClassLoadingException e) {
        LOG.packageNotFound(packageName);
        return;
    } catch (ClassNotFoundException cnf) {
        LOG.packageNotFound(packageName);
        return;
    }
    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));
        }
    }
    bindGenericGenerators(pckg, context);
    bindQueries(pckg, context);
    bindFilterDefs(pckg, context);
    bindTypeDefs(pckg, context);
    bindFetchProfiles(pckg, context);
    BinderHelper.bindAnyMetaDefs(pckg, context);
}
Also used : SequenceGenerators(javax.persistence.SequenceGenerators) ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) SequenceGenerator(javax.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) TableGenerators(javax.persistence.TableGenerators) XPackage(org.hibernate.annotations.common.reflection.XPackage) TableGenerator(javax.persistence.TableGenerator)

Example 2 with SequenceGenerator

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

the class AnnotationBinder method buildGenerators.

private static HashMap<String, IdentifierGeneratorDefinition> buildGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) {
    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);
        }
    }
    SequenceGenerators sequenceGenerators = annElt.getAnnotation(SequenceGenerators.class);
    if (sequenceGenerators != null) {
        for (SequenceGenerator sequenceGenerator : sequenceGenerators.value()) {
            IdentifierGeneratorDefinition idGenerator = buildIdGenerator(sequenceGenerator, context);
            generators.put(idGenerator.getName(), 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);
    }
    if (seqGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(seqGen, context);
        generators.put(idGen.getName(), idGen);
    }
    if (genGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(genGen, context);
        generators.put(idGen.getName(), idGen);
    }
    generators.forEach((name, idGenerator) -> {
        context.getMetadataCollector().addIdentifierGenerator(idGenerator);
    });
    return generators;
}
Also used : SequenceGenerators(javax.persistence.SequenceGenerators) HashMap(java.util.HashMap) SequenceGenerator(javax.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) TableGenerators(javax.persistence.TableGenerators) TableGenerator(javax.persistence.TableGenerator) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 3 with SequenceGenerator

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

the class AnnotationBinder method buildLocalGenerators.

private static HashMap<String, IdentifierGeneratorDefinition> buildLocalGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) {
    HashMap<String, IdentifierGeneratorDefinition> generators = new HashMap<String, IdentifierGeneratorDefinition>();
    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);
    }
    if (seqGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(seqGen, context);
        generators.put(idGen.getName(), idGen);
    }
    if (genGen != null) {
        IdentifierGeneratorDefinition idGen = buildIdGenerator(genGen, context);
        generators.put(idGen.getName(), idGen);
    }
    return generators;
}
Also used : HashMap(java.util.HashMap) SequenceGenerator(javax.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) TableGenerator(javax.persistence.TableGenerator) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 4 with SequenceGenerator

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

the class JPAMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (defaults == null) {
        defaults = new HashMap<>();
        XMLContext.Default xmlDefaults = xmlContext.getDefault(null);
        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 (Element element : xmlContext.getAllDocuments()) {
            @SuppressWarnings("unchecked") List<Element> elements = element.elements("sequence-generator");
            List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
            if (sequenceGenerators == null) {
                sequenceGenerators = new ArrayList<>();
                defaults.put(SequenceGenerator.class, sequenceGenerators);
            }
            for (Element subelement : elements) {
                sequenceGenerators.add(JPAOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(subelement));
            }
            elements = element.elements("table-generator");
            List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
            if (tableGenerators == null) {
                tableGenerators = new ArrayList<>();
                defaults.put(TableGenerator.class, tableGenerators);
            }
            for (Element subelement : elements) {
                tableGenerators.add(JPAOverriddenAnnotationReader.buildTableGeneratorAnnotation(subelement, xmlDefaults));
            }
            List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
            if (namedQueries == null) {
                namedQueries = new ArrayList<>();
                defaults.put(NamedQuery.class, namedQueries);
            }
            List<NamedQuery> currentNamedQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, false, 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 = JPAOverriddenAnnotationReader.buildNamedQueries(element, true, 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 = JPAOverriddenAnnotationReader.buildSqlResultsetMappings(element, 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 = JPAOverriddenAnnotationReader.buildNamedStoreProcedureQueries(element, xmlDefaults, classLoaderAccess);
            namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
        }
    }
    return defaults;
}
Also used : NamedNativeQuery(javax.persistence.NamedNativeQuery) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) SequenceGenerator(javax.persistence.SequenceGenerator) TableGenerator(javax.persistence.TableGenerator) NamedQuery(javax.persistence.NamedQuery) SqlResultSetMapping(javax.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Example 5 with SequenceGenerator

use of javax.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 (context.getMappingDefaults().getImplicitSchemaName() != null) {
        definitionBuilder.addParam(PersistentIdentifierGenerator.SCHEMA, context.getMappingDefaults().getImplicitSchemaName());
    }
    if (context.getMappingDefaults().getImplicitCatalogName() != null) {
        definitionBuilder.addParam(PersistentIdentifierGenerator.CATALOG, context.getMappingDefaults().getImplicitCatalogName());
    }
    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(javax.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) Parameter(org.hibernate.annotations.Parameter) TableGenerator(javax.persistence.TableGenerator) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Aggregations

SequenceGenerator (javax.persistence.SequenceGenerator)6 TableGenerator (javax.persistence.TableGenerator)6 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)5 GenericGenerator (org.hibernate.annotations.GenericGenerator)3 HashMap (java.util.HashMap)2 SequenceGenerators (javax.persistence.SequenceGenerators)2 TableGenerators (javax.persistence.TableGenerators)2 Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GeneratedValue (javax.persistence.GeneratedValue)1 GenerationType (javax.persistence.GenerationType)1 Index (javax.persistence.Index)1 NamedNativeQuery (javax.persistence.NamedNativeQuery)1 NamedQuery (javax.persistence.NamedQuery)1 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)1 SqlResultSetMapping (javax.persistence.SqlResultSetMapping)1 UniqueConstraint (javax.persistence.UniqueConstraint)1 Element (org.dom4j.Element)1