Search in sources :

Example 1 with TableGenerator

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

the class AnnotationBinder method bindPackage.

public static void bindPackage(String packageName, MetadataBuildingContext context) {
    XPackage pckg;
    try {
        pckg = context.getBuildingOptions().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(TableGenerator.class)) {
        TableGenerator ann = pckg.getAnnotation(TableGenerator.class);
        IdentifierGeneratorDefinition idGen = buildIdGenerator(ann, context);
        context.getMetadataCollector().addIdentifierGenerator(idGen);
    }
    bindGenericGenerators(pckg, context);
    bindQueries(pckg, context);
    bindFilterDefs(pckg, context);
    bindTypeDefs(pckg, context);
    bindFetchProfiles(pckg, context);
    BinderHelper.bindAnyMetaDefs(pckg, context);
}
Also used : ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) SequenceGenerator(javax.persistence.SequenceGenerator) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) XPackage(org.hibernate.annotations.common.reflection.XPackage) TableGenerator(javax.persistence.TableGenerator)

Example 2 with TableGenerator

use of javax.persistence.TableGenerator in project cloudstack by apache.

the class SqlGenerator method buildAttributes.

protected void buildAttributes(Class<?> clazz, String tableName, AttributeOverride[] overrides, boolean embedded, boolean isId) {
    if (!embedded && clazz.getAnnotation(Entity.class) == null) {
        // except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself
        if (clazz.getAnnotation(MappedSuperclass.class) != null) {
            Field[] declaredFields = clazz.getDeclaredFields();
            _mappedSuperclassFields = (Field[]) ArrayUtils.addAll(_mappedSuperclassFields, declaredFields);
        }
        return;
    }
    Class<?> parent = clazz.getSuperclass();
    if (parent != null) {
        buildAttributes(parent, DbUtil.getTableName(parent), DbUtil.getAttributeOverrides(parent), false, false);
    }
    if (!embedded) {
        _tables.add(clazz);
        _ids.put(tableName, new ArrayList<Attribute>());
    }
    Field[] fields = clazz.getDeclaredFields();
    fields = (Field[]) ArrayUtils.addAll(fields, _mappedSuperclassFields);
    _mappedSuperclassFields = null;
    for (Field field : fields) {
        field.setAccessible(true);
        TableGenerator tg = field.getAnnotation(TableGenerator.class);
        if (tg != null) {
            _generators.put(field.getName(), tg);
        }
        if (!DbUtil.isPersistable(field)) {
            continue;
        }
        if (field.getAnnotation(Embedded.class) != null) {
            _embeddeds.add(field);
            Class<?> embeddedClass = field.getType();
            assert (embeddedClass.getAnnotation(Embeddable.class) != null) : "Class is not annotated with Embeddable: " + embeddedClass.getName();
            buildAttributes(embeddedClass, tableName, DbUtil.getAttributeOverrides(field), true, false);
            continue;
        }
        if (field.getAnnotation(EmbeddedId.class) != null) {
            _embeddeds.add(field);
            Class<?> embeddedClass = field.getType();
            assert (embeddedClass.getAnnotation(Embeddable.class) != null) : "Class is not annotated with Embeddable: " + embeddedClass.getName();
            buildAttributes(embeddedClass, tableName, DbUtil.getAttributeOverrides(field), true, true);
            continue;
        }
        Attribute attr = new Attribute(clazz, overrides, field, tableName, embedded, isId);
        if (attr.getColumnName().equals(GenericDao.REMOVED_COLUMN)) {
            attr.setTrue(Attribute.Flag.DaoGenerated);
            attr.setFalse(Attribute.Flag.Insertable);
            attr.setFalse(Attribute.Flag.Updatable);
            attr.setTrue(Attribute.Flag.TimeStamp);
            attr.setFalse(Attribute.Flag.Time);
            attr.setFalse(Attribute.Flag.Date);
            attr.setTrue(Attribute.Flag.Nullable);
            attr.setTrue(Attribute.Flag.Removed);
        }
        if (attr.isId()) {
            List<Attribute> attrs = _ids.get(tableName);
            attrs.add(attr);
        }
        _attributes.add(attr);
    }
}
Also used : Field(java.lang.reflect.Field) MappedSuperclass(javax.persistence.MappedSuperclass) EmbeddedId(javax.persistence.EmbeddedId) TableGenerator(javax.persistence.TableGenerator) Embedded(javax.persistence.Embedded)

Example 3 with TableGenerator

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

the class JPAMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (defaults == null) {
        defaults = new HashMap<Object, Object>();
        XMLContext.Default xmlDefaults = xmlContext.getDefault(null);
        defaults.put("schema", xmlDefaults.getSchema());
        defaults.put("catalog", xmlDefaults.getCatalog());
        defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
        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<SequenceGenerator>();
                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<TableGenerator>();
                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<NamedQuery>();
                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<NamedNativeQuery>();
                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<SqlResultSetMapping>();
                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<NamedStoredProcedureQuery>();
                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 4 with TableGenerator

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

the class AnnotationBinder method buildIdGenerator.

private static IdentifierGeneratorDefinition buildIdGenerator(java.lang.annotation.Annotation ann, MetadataBuildingContext context) {
    if (ann == 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 (ann instanceof TableGenerator) {
        context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator((TableGenerator) ann, definitionBuilder);
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Add table generator with name: {0}", definitionBuilder.getName());
        }
    } else if (ann instanceof SequenceGenerator) {
        context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretSequenceGenerator((SequenceGenerator) ann, definitionBuilder);
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Add sequence generator with name: {0}", definitionBuilder.getName());
        }
    } else if (ann instanceof GenericGenerator) {
        GenericGenerator genGen = (GenericGenerator) ann;
        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: " + ann);
    }
    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)

Example 5 with TableGenerator

use of javax.persistence.TableGenerator 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)

Aggregations

TableGenerator (javax.persistence.TableGenerator)6 SequenceGenerator (javax.persistence.SequenceGenerator)4 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)3 Field (java.lang.reflect.Field)2 Embedded (javax.persistence.Embedded)2 EmbeddedId (javax.persistence.EmbeddedId)2 GenericGenerator (org.hibernate.annotations.GenericGenerator)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MappedSuperclass (javax.persistence.MappedSuperclass)1 NamedNativeQuery (javax.persistence.NamedNativeQuery)1 NamedQuery (javax.persistence.NamedQuery)1 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)1 SqlResultSetMapping (javax.persistence.SqlResultSetMapping)1 Element (org.dom4j.Element)1 AssertionFailure (org.hibernate.AssertionFailure)1 Parameter (org.hibernate.annotations.Parameter)1 ClassLoadingException (org.hibernate.annotations.common.reflection.ClassLoadingException)1