Search in sources :

Example 1 with IdentifierGeneratorDefinition

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

the class IdentifierGeneratorDefinitionBinder method processIdentifierGeneratorDefinition.

public static void processIdentifierGeneratorDefinition(HbmLocalMetadataBuildingContext context, JaxbHbmIdentifierGeneratorDefinitionType identifierGenerator) {
    log.debugf("Processing <identifier-generator/> : %s", identifierGenerator.getName());
    context.getMetadataCollector().addIdentifierGenerator(new IdentifierGeneratorDefinition(identifierGenerator.getName(), identifierGenerator.getClazz()));
}
Also used : IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition)

Example 2 with IdentifierGeneratorDefinition

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

the class ModelBinder method makeIdentifier.

private void makeIdentifier(final MappingDocument sourceDocument, IdentifierGeneratorDefinition generator, String unsavedValue, SimpleValue identifierValue) {
    if (generator != null) {
        String generatorName = generator.getStrategy();
        Properties params = new Properties();
        // see if the specified generator name matches a registered <identifier-generator/>
        IdentifierGeneratorDefinition generatorDef = sourceDocument.getMetadataCollector().getIdentifierGenerator(generatorName);
        if (generatorDef != null) {
            generatorName = generatorDef.getStrategy();
            params.putAll(generatorDef.getParameters());
        }
        identifierValue.setIdentifierGeneratorStrategy(generatorName);
        // YUCK!  but cannot think of a clean way to do this given the string-config based scheme
        params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, objectNameNormalizer);
        if (database.getDefaultNamespace().getPhysicalName().getSchema() != null) {
            params.setProperty(PersistentIdentifierGenerator.SCHEMA, database.getDefaultNamespace().getPhysicalName().getSchema().render(database.getDialect()));
        }
        if (database.getDefaultNamespace().getPhysicalName().getCatalog() != null) {
            params.setProperty(PersistentIdentifierGenerator.CATALOG, database.getDefaultNamespace().getPhysicalName().getCatalog().render(database.getDialect()));
        }
        params.putAll(generator.getParameters());
        identifierValue.setIdentifierGeneratorProperties(params);
    }
    identifierValue.getTable().setIdentifierValue(identifierValue);
    if (StringHelper.isNotEmpty(unsavedValue)) {
        identifierValue.setNullValue(unsavedValue);
    } else {
        if ("assigned".equals(identifierValue.getIdentifierGeneratorStrategy())) {
            identifierValue.setNullValue("undefined");
        } else {
            identifierValue.setNullValue(null);
        }
    }
}
Also used : IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) Properties(java.util.Properties)

Example 3 with IdentifierGeneratorDefinition

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

the class BinderHelper method makeIdGenerator.

/**
	 * apply an id generator to a SimpleValue
	 */
public static void makeIdGenerator(SimpleValue id, String generatorType, String generatorName, MetadataBuildingContext buildingContext, Map<String, IdentifierGeneratorDefinition> localGenerators) {
    Table table = id.getTable();
    table.setIdentifierValue(id);
    //generator settings
    id.setIdentifierGeneratorStrategy(generatorType);
    Properties params = new Properties();
    //always settable
    params.setProperty(PersistentIdentifierGenerator.TABLE, table.getName());
    final String implicitCatalogName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitCatalogName();
    if (implicitCatalogName != null) {
        params.put(PersistentIdentifierGenerator.CATALOG, implicitCatalogName);
    }
    final String implicitSchemaName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitSchemaName();
    if (implicitSchemaName != null) {
        params.put(PersistentIdentifierGenerator.SCHEMA, implicitSchemaName);
    }
    if (id.getColumnSpan() == 1) {
        params.setProperty(PersistentIdentifierGenerator.PK, ((org.hibernate.mapping.Column) id.getColumnIterator().next()).getName());
    }
    // YUCK!  but cannot think of a clean way to do this given the string-config based scheme
    params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, buildingContext.getObjectNameNormalizer());
    if (!isEmptyAnnotationValue(generatorName)) {
        //we have a named generator
        IdentifierGeneratorDefinition gen = getIdentifierGenerator(generatorName, localGenerators, buildingContext);
        if (gen == null) {
            throw new AnnotationException("Unknown Id.generator: " + generatorName);
        }
        //This is quite vague in the spec but a generator could override the generate choice
        String identifierGeneratorStrategy = gen.getStrategy();
        //yuk! this is a hack not to override 'AUTO' even if generator is set
        final boolean avoidOverriding = identifierGeneratorStrategy.equals("identity") || identifierGeneratorStrategy.equals("seqhilo") || identifierGeneratorStrategy.equals(MultipleHiLoPerTableGenerator.class.getName());
        if (generatorType == null || !avoidOverriding) {
            id.setIdentifierGeneratorStrategy(identifierGeneratorStrategy);
        }
        //checkIfMatchingGenerator(gen, generatorType, generatorName);
        for (Object o : gen.getParameters().entrySet()) {
            Map.Entry elt = (Map.Entry) o;
            params.setProperty((String) elt.getKey(), (String) elt.getValue());
        }
    }
    if ("assigned".equals(generatorType)) {
        id.setNullValue("undefined");
    }
    id.setIdentifierGeneratorProperties(params);
}
Also used : Table(org.hibernate.mapping.Table) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) AnnotationException(org.hibernate.AnnotationException) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with IdentifierGeneratorDefinition

use of org.hibernate.boot.model.IdentifierGeneratorDefinition 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 5 with IdentifierGeneratorDefinition

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

the class AnnotationBinder method bindClass.

/**
 * Bind a class having JSR175 annotations. Subclasses <b>have to</b> be bound after its parent class.
 *
 * @param clazzToProcess entity to bind as {@code XClass} instance
 * @param inheritanceStatePerClass Meta data about the inheritance relationships for all mapped classes
 *
 * @throws MappingException in case there is a configuration error
 */
public static void bindClass(XClass clazzToProcess, Map<XClass, InheritanceState> inheritanceStatePerClass, MetadataBuildingContext context) throws MappingException {
    // @Entity and @MappedSuperclass on the same class leads to a NPE down the road
    if (clazzToProcess.isAnnotationPresent(Entity.class) && clazzToProcess.isAnnotationPresent(MappedSuperclass.class)) {
        throw new AnnotationException("An entity cannot be annotated with both @Entity and @MappedSuperclass: " + clazzToProcess.getName());
    }
    // TODO: be more strict with secondarytable allowance (not for ids, not for secondary table join columns etc)
    InheritanceState inheritanceState = inheritanceStatePerClass.get(clazzToProcess);
    AnnotatedClassType classType = context.getMetadataCollector().getClassType(clazzToProcess);
    // Queries declared in MappedSuperclass should be usable in Subclasses
    if (AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals(classType)) {
        bindQueries(clazzToProcess, context);
        bindTypeDefs(clazzToProcess, context);
        bindFilterDefs(clazzToProcess, context);
    }
    if (!isEntityClassType(clazzToProcess, classType)) {
        return;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding entity from annotated class: %s", clazzToProcess.getName());
    }
    PersistentClass superEntity = getSuperEntity(clazzToProcess, inheritanceStatePerClass, context, inheritanceState);
    PersistentClass persistentClass = makePersistentClass(inheritanceState, superEntity, context);
    Entity entityAnn = clazzToProcess.getAnnotation(Entity.class);
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(org.hibernate.annotations.Entity.class);
    EntityBinder entityBinder = new EntityBinder(entityAnn, hibEntityAnn, clazzToProcess, persistentClass, context);
    entityBinder.setInheritanceState(inheritanceState);
    bindQueries(clazzToProcess, context);
    bindFilterDefs(clazzToProcess, context);
    bindTypeDefs(clazzToProcess, context);
    bindFetchProfiles(clazzToProcess, context);
    BinderHelper.bindAnyMetaDefs(clazzToProcess, context);
    String schema = "";
    // might be no @Table annotation on the annotated class
    String table = "";
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<>();
    javax.persistence.Table tabAnn = null;
    if (clazzToProcess.isAnnotationPresent(javax.persistence.Table.class)) {
        tabAnn = clazzToProcess.getAnnotation(javax.persistence.Table.class);
        table = tabAnn.name();
        schema = tabAnn.schema();
        catalog = tabAnn.catalog();
        uniqueConstraints = TableBinder.buildUniqueConstraintHolders(tabAnn.uniqueConstraints());
    }
    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(clazzToProcess, context, inheritanceState, superEntity);
    final Ejb3DiscriminatorColumn discriminatorColumn;
    if (InheritanceType.SINGLE_TABLE.equals(inheritanceState.getType())) {
        discriminatorColumn = processSingleTableDiscriminatorProperties(clazzToProcess, context, inheritanceState, entityBinder);
    } else if (InheritanceType.JOINED.equals(inheritanceState.getType())) {
        discriminatorColumn = processJoinedDiscriminatorProperties(clazzToProcess, context, inheritanceState, entityBinder);
    } else {
        discriminatorColumn = null;
    }
    entityBinder.setProxy(clazzToProcess.getAnnotation(Proxy.class));
    entityBinder.setBatchSize(clazzToProcess.getAnnotation(BatchSize.class));
    entityBinder.setWhere(clazzToProcess.getAnnotation(Where.class));
    applyCacheSettings(entityBinder, clazzToProcess, context);
    bindFilters(clazzToProcess, entityBinder, context);
    entityBinder.bindEntity();
    if (inheritanceState.hasTable()) {
        Check checkAnn = clazzToProcess.getAnnotation(Check.class);
        String constraints = checkAnn == null ? null : checkAnn.constraints();
        EntityTableXref denormalizedTableXref = inheritanceState.hasDenormalizedTable() ? context.getMetadataCollector().getEntityTableXref(superEntity.getEntityName()) : null;
        entityBinder.bindTable(schema, catalog, table, uniqueConstraints, constraints, denormalizedTableXref);
    } else {
        if (clazzToProcess.isAnnotationPresent(Table.class)) {
            LOG.invalidTableAnnotation(clazzToProcess.getName());
        }
        if (inheritanceState.getType() == InheritanceType.SINGLE_TABLE) {
            // we at least need to properly set up the EntityTableXref
            entityBinder.bindTableForDiscriminatedSubclass(context.getMetadataCollector().getEntityTableXref(superEntity.getEntityName()));
        }
    }
    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(clazzToProcess, persistentClass, entityBinder, context, inheritanceStatePerClass);
    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(javax.persistence.SecondaryTable.class);
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(javax.persistence.SecondaryTables.class);
    entityBinder.firstLevelSecondaryTablesBinding(secTabAnn, secTabsAnn);
    OnDelete onDeleteAnn = clazzToProcess.getAnnotation(OnDelete.class);
    boolean onDeleteAppropriate = false;
    // todo : sucks that this is separate from RootClass distinction
    final boolean isInheritanceRoot = !inheritanceState.hasParents();
    final boolean hasSubclasses = inheritanceState.hasSiblings();
    if (InheritanceType.JOINED.equals(inheritanceState.getType())) {
        if (inheritanceState.hasParents()) {
            onDeleteAppropriate = true;
            final JoinedSubclass jsc = (JoinedSubclass) persistentClass;
            SimpleValue key = new DependantValue(context, jsc.getTable(), jsc.getIdentifier());
            jsc.setKey(key);
            ForeignKey fk = clazzToProcess.getAnnotation(ForeignKey.class);
            if (fk != null && !BinderHelper.isEmptyAnnotationValue(fk.name())) {
                key.setForeignKeyName(fk.name());
            } else {
                final PrimaryKeyJoinColumn pkJoinColumn = clazzToProcess.getAnnotation(PrimaryKeyJoinColumn.class);
                final PrimaryKeyJoinColumns pkJoinColumns = clazzToProcess.getAnnotation(PrimaryKeyJoinColumns.class);
                if (pkJoinColumns != null && pkJoinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
                    // don't apply a constraint based on ConstraintMode
                    key.setForeignKeyName("none");
                } else if (pkJoinColumns != null && !StringHelper.isEmpty(pkJoinColumns.foreignKey().name())) {
                    key.setForeignKeyName(pkJoinColumns.foreignKey().name());
                } else if (pkJoinColumn != null && pkJoinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
                    // don't apply a constraint based on ConstraintMode
                    key.setForeignKeyName("none");
                } else if (pkJoinColumn != null && !StringHelper.isEmpty(pkJoinColumn.foreignKey().name())) {
                    key.setForeignKeyName(pkJoinColumn.foreignKey().name());
                }
            }
            if (onDeleteAnn != null) {
                key.setCascadeDeleteEnabled(OnDeleteAction.CASCADE.equals(onDeleteAnn.action()));
            } else {
                key.setCascadeDeleteEnabled(false);
            }
            // we are never in a second pass at that stage, so queue it
            context.getMetadataCollector().addSecondPass(new JoinedSubclassFkSecondPass(jsc, inheritanceJoinedColumns, key, context));
            context.getMetadataCollector().addSecondPass(new CreateKeySecondPass(jsc));
        }
        if (isInheritanceRoot) {
            // (it is perfectly valid for joined subclasses to not have discriminators).
            if (discriminatorColumn != null) {
                // we have a discriminator column
                if (hasSubclasses || !discriminatorColumn.isImplicit()) {
                    bindDiscriminatorColumnToRootPersistentClass((RootClass) persistentClass, discriminatorColumn, entityBinder.getSecondaryTables(), propertyHolder, context);
                    // bind it again since the type might have changed
                    entityBinder.bindDiscriminatorValue();
                }
            }
        }
    } else if (InheritanceType.SINGLE_TABLE.equals(inheritanceState.getType())) {
        if (isInheritanceRoot) {
            if (hasSubclasses || !discriminatorColumn.isImplicit()) {
                bindDiscriminatorColumnToRootPersistentClass((RootClass) persistentClass, discriminatorColumn, entityBinder.getSecondaryTables(), propertyHolder, context);
                // bind it again since the type might have changed
                entityBinder.bindDiscriminatorValue();
            }
        }
    }
    if (onDeleteAnn != null && !onDeleteAppropriate) {
        LOG.invalidOnDeleteAnnotation(propertyHolder.getEntityName());
    }
    // try to find class level generators
    HashMap<String, IdentifierGeneratorDefinition> classGenerators = buildGenerators(clazzToProcess, context);
    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess(persistentClass, entityBinder);
    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<>();
    boolean isIdClass = mapAsIdClass(inheritanceStatePerClass, inheritanceState, persistentClass, entityBinder, propertyHolder, elementsToProcess, idPropertiesIfIdClass, context);
    if (!isIdClass) {
        entityBinder.setWrapIdsInEmbeddedComponents(elementsToProcess.getIdPropertyCount() > 1);
    }
    processIdPropertiesIfNotAlready(inheritanceStatePerClass, context, persistentClass, entityBinder, propertyHolder, classGenerators, elementsToProcess, subclassAndSingleTableStrategy, idPropertiesIfIdClass);
    if (!inheritanceState.hasParents()) {
        final RootClass rootClass = (RootClass) persistentClass;
        context.getMetadataCollector().addSecondPass(new CreateKeySecondPass(rootClass));
    } else {
        superEntity.addSubclass((Subclass) persistentClass);
    }
    context.getMetadataCollector().addEntityBinding(persistentClass);
    // Process secondary tables and complementary definitions (ie o.h.a.Table)
    context.getMetadataCollector().addSecondPass(new SecondaryTableSecondPass(entityBinder, propertyHolder, clazzToProcess));
    // add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions(clazzToProcess.getAnnotation(org.hibernate.annotations.Table.class));
    entityBinder.processComplementaryTableDefinitions(clazzToProcess.getAnnotation(org.hibernate.annotations.Tables.class));
    entityBinder.processComplementaryTableDefinitions(tabAnn);
}
Also used : Entity(javax.persistence.Entity) ArrayList(java.util.ArrayList) Check(org.hibernate.annotations.Check) Proxy(org.hibernate.annotations.Proxy) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) Table(javax.persistence.Table) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) AnnotationException(org.hibernate.AnnotationException) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) PersistentClass(org.hibernate.mapping.PersistentClass) HashSet(java.util.HashSet) RootClass(org.hibernate.mapping.RootClass) BatchSize(org.hibernate.annotations.BatchSize) Table(javax.persistence.Table) CollectionTable(javax.persistence.CollectionTable) JoinTable(javax.persistence.JoinTable) DependantValue(org.hibernate.mapping.DependantValue) ForeignKey(org.hibernate.annotations.ForeignKey) SimpleValue(org.hibernate.mapping.SimpleValue) MappedSuperclass(javax.persistence.MappedSuperclass) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) EntityBinder(org.hibernate.cfg.annotations.EntityBinder) Where(org.hibernate.annotations.Where) OnDelete(org.hibernate.annotations.OnDelete)

Aggregations

IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)12 HashMap (java.util.HashMap)5 SequenceGenerator (javax.persistence.SequenceGenerator)5 TableGenerator (javax.persistence.TableGenerator)5 AnnotationException (org.hibernate.AnnotationException)4 Properties (java.util.Properties)3 Map (java.util.Map)2 JoinTable (javax.persistence.JoinTable)2 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)2 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)2 SequenceGenerators (javax.persistence.SequenceGenerators)2 TableGenerators (javax.persistence.TableGenerators)2 UniqueConstraint (javax.persistence.UniqueConstraint)2 GenericGenerator (org.hibernate.annotations.GenericGenerator)2 OnDelete (org.hibernate.annotations.OnDelete)2 RootClass (org.hibernate.mapping.RootClass)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 Table (org.hibernate.mapping.Table)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1