Search in sources :

Example 1 with GenericGenerator

use of org.hibernate.annotations.GenericGenerator in project hibernate-orm by hibernate.

the class AnnotationBinder method bindGenericGenerators.

private static void bindGenericGenerators(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
    GenericGenerator defAnn = annotatedElement.getAnnotation(GenericGenerator.class);
    GenericGenerators defsAnn = annotatedElement.getAnnotation(GenericGenerators.class);
    if (defAnn != null) {
        bindGenericGenerator(defAnn, context);
    }
    if (defsAnn != null) {
        for (GenericGenerator def : defsAnn.value()) {
            bindGenericGenerator(def, context);
        }
    }
}
Also used : GenericGenerators(org.hibernate.annotations.GenericGenerators) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 2 with GenericGenerator

use of org.hibernate.annotations.GenericGenerator 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 GenericGenerator

use of org.hibernate.annotations.GenericGenerator in project tephra by heisedebaise.

the class ModelTablesImpl method parse.

private void parse(Class<? extends Model> modelClass) {
    Table table = modelClass.getAnnotation(Table.class);
    if (table == null)
        return;
    ModelTable modelTable = BeanFactory.getBean(ModelTable.class);
    modelTable.setModelClass(modelClass);
    DataSource dataSource = modelClass.getAnnotation(DataSource.class);
    if (dataSource != null)
        modelTable.setDataSource(dataSource.key());
    modelTable.setTableName(table.name());
    Memory memory = modelClass.getAnnotation(Memory.class);
    if (memory != null)
        modelTable.setMemoryName(memory.name());
    Method[] methods = modelClass.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (name.length() < 3)
            continue;
        if (name.equals("getId")) {
            modelTable.setIdColumnName(method.getAnnotation(Column.class).name());
            GenericGenerator generator = method.getAnnotation(GenericGenerator.class);
            if (generator != null && generator.name().toLowerCase().startsWith("uuid"))
                modelTable.setUuid(true);
            continue;
        }
        String propertyName = converter.toFirstLowerCase(name.substring(3));
        if (startsWith(name, 'g')) {
            modelTable.addGetMethod(propertyName, method);
            Column column = method.getAnnotation(Column.class);
            if (column != null)
                modelTable.addColumn(propertyName, column);
            continue;
        }
        if (startsWith(name, 's'))
            modelTable.addSetMethod(propertyName, method);
    }
    map.put(modelClass, modelTable);
}
Also used : Table(javax.persistence.Table) Column(javax.persistence.Column) Method(java.lang.reflect.Method) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 4 with GenericGenerator

use of org.hibernate.annotations.GenericGenerator 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 5 with GenericGenerator

use of org.hibernate.annotations.GenericGenerator 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

GenericGenerator (org.hibernate.annotations.GenericGenerator)6 TableGenerator (javax.persistence.TableGenerator)4 SequenceGenerator (javax.persistence.SequenceGenerator)3 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)3 HashMap (java.util.HashMap)2 Parameter (org.hibernate.annotations.Parameter)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Column (javax.persistence.Column)1 SequenceGenerators (javax.persistence.SequenceGenerators)1 Table (javax.persistence.Table)1 TableGenerators (javax.persistence.TableGenerators)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 AssertionFailure (org.hibernate.AssertionFailure)1 SessionFactory (org.hibernate.SessionFactory)1 GenericGenerators (org.hibernate.annotations.GenericGenerators)1 ClassMetadata (org.hibernate.metadata.ClassMetadata)1