Search in sources :

Example 1 with LazyGroup

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

the class AnnotationBinder method processElementAnnotations.

/*
	 * Process annotation of a particular property
	 */
private static void processElementAnnotations(PropertyHolder propertyHolder, Nullability nullability, PropertyData inferredData, HashMap<String, IdentifierGeneratorDefinition> classGenerators, EntityBinder entityBinder, boolean isIdentifierMapper, boolean isComponentEmbedded, boolean inSecondPass, MetadataBuildingContext context, Map<XClass, InheritanceState> inheritanceStatePerClass) throws MappingException {
    if (!propertyHolder.isComponent()) {
        if (entityBinder.isPropertyDefinedInSuperHierarchy(inferredData.getPropertyName())) {
            LOG.debugf("Skipping attribute [%s : %s] as it was already processed as part of super hierarchy", inferredData.getClassOrElementName(), inferredData.getPropertyName());
            return;
        }
    }
    /**
     * inSecondPass can only be used to apply right away the second pass of a composite-element
     * Because it's a value type, there is no bidirectional association, hence second pass
     * ordering does not matter
     */
    final boolean traceEnabled = LOG.isTraceEnabled();
    if (traceEnabled) {
        LOG.tracev("Processing annotations of {0}.{1}", propertyHolder.getEntityName(), inferredData.getPropertyName());
    }
    final XProperty property = inferredData.getProperty();
    if (property.isAnnotationPresent(Parent.class)) {
        if (propertyHolder.isComponent()) {
            propertyHolder.setParentProperty(property.getName());
        } else {
            throw new AnnotationException("@Parent cannot be applied outside an embeddable object: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
        return;
    }
    ColumnsBuilder columnsBuilder = new ColumnsBuilder(propertyHolder, nullability, property, inferredData, entityBinder, context).extractMetadata();
    Ejb3Column[] columns = columnsBuilder.getColumns();
    Ejb3JoinColumn[] joinColumns = columnsBuilder.getJoinColumns();
    final XClass returnedClass = inferredData.getClassOrElement();
    // prepare PropertyBinder
    PropertyBinder propertyBinder = new PropertyBinder();
    propertyBinder.setName(inferredData.getPropertyName());
    propertyBinder.setReturnedClassName(inferredData.getTypeName());
    propertyBinder.setAccessType(inferredData.getDefaultAccess());
    propertyBinder.setHolder(propertyHolder);
    propertyBinder.setProperty(property);
    propertyBinder.setReturnedClass(inferredData.getPropertyClass());
    propertyBinder.setBuildingContext(context);
    if (isIdentifierMapper) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    }
    propertyBinder.setDeclaringClass(inferredData.getDeclaringClass());
    propertyBinder.setEntityBinder(entityBinder);
    propertyBinder.setInheritanceStatePerClass(inheritanceStatePerClass);
    boolean isId = !entityBinder.isIgnoreIdAnnotations() && (property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(EmbeddedId.class));
    propertyBinder.setId(isId);
    final LazyGroup lazyGroupAnnotation = property.getAnnotation(LazyGroup.class);
    if (lazyGroupAnnotation != null) {
        propertyBinder.setLazyGroup(lazyGroupAnnotation.value());
    }
    if (property.isAnnotationPresent(Version.class)) {
        if (isIdentifierMapper) {
            throw new AnnotationException("@IdClass class should not have @Version property");
        }
        if (!(propertyHolder.getPersistentClass() instanceof RootClass)) {
            throw new AnnotationException("Unable to define/override @Version on a subclass: " + propertyHolder.getEntityName());
        }
        if (!propertyHolder.isEntity()) {
            throw new AnnotationException("Unable to define @Version on an embedded class: " + propertyHolder.getEntityName());
        }
        if (traceEnabled) {
            LOG.tracev("{0} is a version property", inferredData.getPropertyName());
        }
        RootClass rootClass = (RootClass) propertyHolder.getPersistentClass();
        propertyBinder.setColumns(columns);
        Property prop = propertyBinder.makePropertyValueAndBind();
        setVersionInformation(property, propertyBinder);
        rootClass.setVersion(prop);
        // If version is on a mapped superclass, update the mapping
        final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(inferredData.getDeclaringClass(), inheritanceStatePerClass, context);
        if (superclass != null) {
            superclass.setDeclaredVersion(prop);
        } else {
            // we know the property is on the actual entity
            rootClass.setDeclaredVersion(prop);
        }
        SimpleValue simpleValue = (SimpleValue) prop.getValue();
        simpleValue.setNullValue("undefined");
        rootClass.setOptimisticLockStyle(OptimisticLockStyle.VERSION);
        if (traceEnabled) {
            LOG.tracev("Version name: {0}, unsavedValue: {1}", rootClass.getVersion().getName(), ((SimpleValue) rootClass.getVersion().getValue()).getNullValue());
        }
    } else {
        final boolean forcePersist = property.isAnnotationPresent(MapsId.class) || property.isAnnotationPresent(Id.class);
        if (property.isAnnotationPresent(ManyToOne.class)) {
            ManyToOne ann = property.getAnnotation(ManyToOne.class);
            // check validity
            if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
                throw new AnnotationException("@Column(s) not allowed on a @ManyToOne property: " + BinderHelper.getPath(propertyHolder, inferredData));
            }
            Cascade hibernateCascade = property.getAnnotation(Cascade.class);
            NotFound notFound = property.getAnnotation(NotFound.class);
            boolean ignoreNotFound = notFound != null && notFound.action().equals(NotFoundAction.IGNORE);
            OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
            boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals(onDeleteAnn.action());
            JoinTable assocTable = propertyHolder.getJoinTable(property);
            if (assocTable != null) {
                Join join = propertyHolder.addJoin(assocTable, false);
                for (Ejb3JoinColumn joinColumn : joinColumns) {
                    joinColumn.setExplicitTableName(join.getTable().getName());
                }
            }
            final boolean mandatory = !ann.optional() || forcePersist;
            bindManyToOne(getCascadeStrategy(ann.cascade(), hibernateCascade, false, forcePersist), joinColumns, !mandatory, ignoreNotFound, onDeleteCascade, ToOneBinder.getTargetEntity(inferredData, context), propertyHolder, inferredData, false, isIdentifierMapper, inSecondPass, propertyBinder, context);
        } else if (property.isAnnotationPresent(OneToOne.class)) {
            OneToOne ann = property.getAnnotation(OneToOne.class);
            // check validity
            if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
                throw new AnnotationException("@Column(s) not allowed on a @OneToOne property: " + BinderHelper.getPath(propertyHolder, inferredData));
            }
            // FIXME support a proper PKJCs
            boolean trueOneToOne = property.isAnnotationPresent(PrimaryKeyJoinColumn.class) || property.isAnnotationPresent(PrimaryKeyJoinColumns.class);
            Cascade hibernateCascade = property.getAnnotation(Cascade.class);
            NotFound notFound = property.getAnnotation(NotFound.class);
            boolean ignoreNotFound = notFound != null && notFound.action().equals(NotFoundAction.IGNORE);
            OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
            boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals(onDeleteAnn.action());
            JoinTable assocTable = propertyHolder.getJoinTable(property);
            if (assocTable != null) {
                Join join = propertyHolder.addJoin(assocTable, false);
                for (Ejb3JoinColumn joinColumn : joinColumns) {
                    joinColumn.setExplicitTableName(join.getTable().getName());
                }
            }
            // MapsId means the columns belong to the pk => not null
            // @OneToOne with @PKJC can still be optional
            final boolean mandatory = !ann.optional() || forcePersist;
            bindOneToOne(getCascadeStrategy(ann.cascade(), hibernateCascade, ann.orphanRemoval(), forcePersist), joinColumns, !mandatory, getFetchMode(ann.fetch()), ignoreNotFound, onDeleteCascade, ToOneBinder.getTargetEntity(inferredData, context), propertyHolder, inferredData, ann.mappedBy(), trueOneToOne, isIdentifierMapper, inSecondPass, propertyBinder, context);
        } else if (property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
            // check validity
            if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
                throw new AnnotationException("@Column(s) not allowed on a @Any property: " + BinderHelper.getPath(propertyHolder, inferredData));
            }
            Cascade hibernateCascade = property.getAnnotation(Cascade.class);
            OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
            boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals(onDeleteAnn.action());
            JoinTable assocTable = propertyHolder.getJoinTable(property);
            if (assocTable != null) {
                Join join = propertyHolder.addJoin(assocTable, false);
                for (Ejb3JoinColumn joinColumn : joinColumns) {
                    joinColumn.setExplicitTableName(join.getTable().getName());
                }
            }
            bindAny(getCascadeStrategy(null, hibernateCascade, false, forcePersist), // @Any has not cascade attribute
            joinColumns, onDeleteCascade, nullability, propertyHolder, inferredData, entityBinder, isIdentifierMapper, context);
        } else if (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ManyToMany.class) || property.isAnnotationPresent(ElementCollection.class) || property.isAnnotationPresent(ManyToAny.class)) {
            OneToMany oneToManyAnn = property.getAnnotation(OneToMany.class);
            ManyToMany manyToManyAnn = property.getAnnotation(ManyToMany.class);
            ElementCollection elementCollectionAnn = property.getAnnotation(ElementCollection.class);
            if ((oneToManyAnn != null || manyToManyAnn != null || elementCollectionAnn != null) && isToManyAssociationWithinEmbeddableCollection(propertyHolder)) {
                throw new AnnotationException("@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection: " + BinderHelper.getPath(propertyHolder, inferredData));
            }
            final IndexColumn indexColumn;
            if (property.isAnnotationPresent(OrderColumn.class)) {
                indexColumn = IndexColumn.buildColumnFromAnnotation(property.getAnnotation(OrderColumn.class), propertyHolder, inferredData, entityBinder.getSecondaryTables(), context);
                if (property.isAnnotationPresent(ListIndexBase.class)) {
                    indexColumn.setBase((property.getAnnotation(ListIndexBase.class)).value());
                }
            } else {
                // if @IndexColumn is not there, the generated IndexColumn is an implicit column and not used.
                // so we can leave the legacy processing as the default
                indexColumn = IndexColumn.buildColumnFromAnnotation(property.getAnnotation(org.hibernate.annotations.IndexColumn.class), propertyHolder, inferredData, context);
            }
            CollectionBinder collectionBinder = CollectionBinder.getCollectionBinder(propertyHolder.getEntityName(), property, !indexColumn.isImplicit(), property.isAnnotationPresent(MapKeyType.class), context);
            collectionBinder.setIndexColumn(indexColumn);
            collectionBinder.setMapKey(property.getAnnotation(MapKey.class));
            collectionBinder.setPropertyName(inferredData.getPropertyName());
            collectionBinder.setBatchSize(property.getAnnotation(BatchSize.class));
            collectionBinder.setJpaOrderBy(property.getAnnotation(javax.persistence.OrderBy.class));
            collectionBinder.setSqlOrderBy(property.getAnnotation(OrderBy.class));
            collectionBinder.setSort(property.getAnnotation(Sort.class));
            collectionBinder.setNaturalSort(property.getAnnotation(SortNatural.class));
            collectionBinder.setComparatorSort(property.getAnnotation(SortComparator.class));
            Cache cachAnn = property.getAnnotation(Cache.class);
            collectionBinder.setCache(cachAnn);
            collectionBinder.setPropertyHolder(propertyHolder);
            Cascade hibernateCascade = property.getAnnotation(Cascade.class);
            NotFound notFound = property.getAnnotation(NotFound.class);
            boolean ignoreNotFound = notFound != null && notFound.action().equals(NotFoundAction.IGNORE);
            collectionBinder.setIgnoreNotFound(ignoreNotFound);
            collectionBinder.setCollectionType(inferredData.getProperty().getElementClass());
            collectionBinder.setBuildingContext(context);
            collectionBinder.setAccessType(inferredData.getDefaultAccess());
            Ejb3Column[] elementColumns;
            // do not use "element" if you are a JPA 2 @ElementCollection only for legacy Hibernate mappings
            boolean isJPA2ForValueMapping = property.isAnnotationPresent(ElementCollection.class);
            PropertyData virtualProperty = isJPA2ForValueMapping ? inferredData : new WrappedInferredData(inferredData, "element");
            if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Formula.class)) {
                Column ann = property.getAnnotation(Column.class);
                Formula formulaAnn = property.getAnnotation(Formula.class);
                elementColumns = Ejb3Column.buildColumnFromAnnotation(new Column[] { ann }, formulaAnn, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
            } else if (property.isAnnotationPresent(Columns.class)) {
                Columns anns = property.getAnnotation(Columns.class);
                elementColumns = Ejb3Column.buildColumnFromAnnotation(anns.columns(), null, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
            } else {
                elementColumns = Ejb3Column.buildColumnFromAnnotation(null, null, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
            }
            {
                Column[] keyColumns = null;
                // JPA 2 has priority and has different default column values, differenciate legacy from JPA 2
                Boolean isJPA2 = null;
                if (property.isAnnotationPresent(MapKeyColumn.class)) {
                    isJPA2 = Boolean.TRUE;
                    keyColumns = new Column[] { new MapKeyColumnDelegator(property.getAnnotation(MapKeyColumn.class)) };
                }
                // not explicitly legacy
                if (isJPA2 == null) {
                    isJPA2 = Boolean.TRUE;
                }
                // nullify empty array
                keyColumns = keyColumns != null && keyColumns.length > 0 ? keyColumns : null;
                // "mapkey" is the legacy column name of the key column pre JPA 2
                PropertyData mapKeyVirtualProperty = new WrappedInferredData(inferredData, "mapkey");
                Ejb3Column[] mapColumns = Ejb3Column.buildColumnFromAnnotation(keyColumns, null, Nullability.FORCED_NOT_NULL, propertyHolder, isJPA2 ? inferredData : mapKeyVirtualProperty, isJPA2 ? "_KEY" : null, entityBinder.getSecondaryTables(), context);
                collectionBinder.setMapKeyColumns(mapColumns);
            }
            {
                JoinColumn[] joinKeyColumns = null;
                // JPA 2 has priority and has different default column values, differenciate legacy from JPA 2
                Boolean isJPA2 = null;
                if (property.isAnnotationPresent(MapKeyJoinColumns.class)) {
                    isJPA2 = Boolean.TRUE;
                    final MapKeyJoinColumn[] mapKeyJoinColumns = property.getAnnotation(MapKeyJoinColumns.class).value();
                    joinKeyColumns = new JoinColumn[mapKeyJoinColumns.length];
                    int index = 0;
                    for (MapKeyJoinColumn joinColumn : mapKeyJoinColumns) {
                        joinKeyColumns[index] = new MapKeyJoinColumnDelegator(joinColumn);
                        index++;
                    }
                    if (property.isAnnotationPresent(MapKeyJoinColumn.class)) {
                        throw new AnnotationException("@MapKeyJoinColumn and @MapKeyJoinColumns used on the same property: " + BinderHelper.getPath(propertyHolder, inferredData));
                    }
                } else if (property.isAnnotationPresent(MapKeyJoinColumn.class)) {
                    isJPA2 = Boolean.TRUE;
                    joinKeyColumns = new JoinColumn[] { new MapKeyJoinColumnDelegator(property.getAnnotation(MapKeyJoinColumn.class)) };
                }
                // not explicitly legacy
                if (isJPA2 == null) {
                    isJPA2 = Boolean.TRUE;
                }
                PropertyData mapKeyVirtualProperty = new WrappedInferredData(inferredData, "mapkey");
                Ejb3JoinColumn[] mapJoinColumns = Ejb3JoinColumn.buildJoinColumnsWithDefaultColumnSuffix(joinKeyColumns, null, entityBinder.getSecondaryTables(), propertyHolder, isJPA2 ? inferredData.getPropertyName() : mapKeyVirtualProperty.getPropertyName(), isJPA2 ? "_KEY" : null, context);
                collectionBinder.setMapKeyManyToManyColumns(mapJoinColumns);
            }
            // potential element
            collectionBinder.setEmbedded(property.isAnnotationPresent(Embedded.class));
            collectionBinder.setElementColumns(elementColumns);
            collectionBinder.setProperty(property);
            // TODO enhance exception with @ManyToAny and @CollectionOfElements
            if (oneToManyAnn != null && manyToManyAnn != null) {
                throw new AnnotationException("@OneToMany and @ManyToMany on the same property is not allowed: " + propertyHolder.getEntityName() + "." + inferredData.getPropertyName());
            }
            String mappedBy = null;
            if (oneToManyAnn != null) {
                for (Ejb3JoinColumn column : joinColumns) {
                    if (column.isSecondary()) {
                        throw new NotYetImplementedException("Collections having FK in secondary table");
                    }
                }
                collectionBinder.setFkJoinColumns(joinColumns);
                mappedBy = oneToManyAnn.mappedBy();
                collectionBinder.setTargetEntity(context.getBootstrapContext().getReflectionManager().toXClass(oneToManyAnn.targetEntity()));
                collectionBinder.setCascadeStrategy(getCascadeStrategy(oneToManyAnn.cascade(), hibernateCascade, oneToManyAnn.orphanRemoval(), false));
                collectionBinder.setOneToMany(true);
            } else if (elementCollectionAnn != null) {
                for (Ejb3JoinColumn column : joinColumns) {
                    if (column.isSecondary()) {
                        throw new NotYetImplementedException("Collections having FK in secondary table");
                    }
                }
                collectionBinder.setFkJoinColumns(joinColumns);
                mappedBy = "";
                final Class<?> targetElement = elementCollectionAnn.targetClass();
                collectionBinder.setTargetEntity(context.getBootstrapContext().getReflectionManager().toXClass(targetElement));
                // collectionBinder.setCascadeStrategy( getCascadeStrategy( embeddedCollectionAnn.cascade(), hibernateCascade ) );
                collectionBinder.setOneToMany(true);
            } else if (manyToManyAnn != null) {
                mappedBy = manyToManyAnn.mappedBy();
                collectionBinder.setTargetEntity(context.getBootstrapContext().getReflectionManager().toXClass(manyToManyAnn.targetEntity()));
                collectionBinder.setCascadeStrategy(getCascadeStrategy(manyToManyAnn.cascade(), hibernateCascade, false, false));
                collectionBinder.setOneToMany(false);
            } else if (property.isAnnotationPresent(ManyToAny.class)) {
                mappedBy = "";
                collectionBinder.setTargetEntity(context.getBootstrapContext().getReflectionManager().toXClass(void.class));
                collectionBinder.setCascadeStrategy(getCascadeStrategy(null, hibernateCascade, false, false));
                collectionBinder.setOneToMany(false);
            }
            collectionBinder.setMappedBy(mappedBy);
            bindJoinedTableAssociation(property, context, entityBinder, collectionBinder, propertyHolder, inferredData, mappedBy);
            OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
            boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals(onDeleteAnn.action());
            collectionBinder.setCascadeDeleteEnabled(onDeleteCascade);
            if (isIdentifierMapper) {
                collectionBinder.setInsertable(false);
                collectionBinder.setUpdatable(false);
            }
            if (property.isAnnotationPresent(CollectionId.class)) {
                // do not compute the generators unless necessary
                HashMap<String, IdentifierGeneratorDefinition> localGenerators = (HashMap<String, IdentifierGeneratorDefinition>) classGenerators.clone();
                localGenerators.putAll(buildGenerators(property, context));
                collectionBinder.setLocalGenerators(localGenerators);
            }
            collectionBinder.setInheritanceStatePerClass(inheritanceStatePerClass);
            collectionBinder.setDeclaringClass(inferredData.getDeclaringClass());
            collectionBinder.bind();
        } else // Either a regular property or a basic @Id or @EmbeddedId while not ignoring id annotations
        if (!isId || !entityBinder.isIgnoreIdAnnotations()) {
            // define whether the type is a component or not
            boolean isComponent = false;
            // Overrides from @MapsId if needed
            boolean isOverridden = false;
            if (isId || propertyHolder.isOrWithinEmbeddedId() || propertyHolder.isInIdClass()) {
                // the associated entity could be using an @IdClass making the overridden property a component
                final PropertyData overridingProperty = BinderHelper.getPropertyOverriddenByMapperOrMapsId(isId, propertyHolder, property.getName(), context);
                if (overridingProperty != null) {
                    isOverridden = true;
                    final InheritanceState state = inheritanceStatePerClass.get(overridingProperty.getClassOrElement());
                    if (state != null) {
                        isComponent = isComponent || state.hasIdClassOrEmbeddedId();
                    }
                    // Get the new column
                    columns = columnsBuilder.overrideColumnFromMapperOrMapsIdProperty(isId);
                }
            }
            isComponent = isComponent || property.isAnnotationPresent(Embedded.class) || property.isAnnotationPresent(EmbeddedId.class) || returnedClass.isAnnotationPresent(Embeddable.class);
            if (isComponent) {
                String referencedEntityName = null;
                if (isOverridden) {
                    final PropertyData mapsIdProperty = BinderHelper.getPropertyOverriddenByMapperOrMapsId(isId, propertyHolder, property.getName(), context);
                    referencedEntityName = mapsIdProperty.getClassOrElementName();
                }
                AccessType propertyAccessor = entityBinder.getPropertyAccessor(property);
                propertyBinder = bindComponent(inferredData, propertyHolder, propertyAccessor, entityBinder, isIdentifierMapper, context, isComponentEmbedded, isId, inheritanceStatePerClass, referencedEntityName, isOverridden ? (Ejb3JoinColumn[]) columns : null);
            } else {
                // provide the basic property mapping
                boolean optional = true;
                boolean lazy = false;
                if (property.isAnnotationPresent(Basic.class)) {
                    Basic ann = property.getAnnotation(Basic.class);
                    optional = ann.optional();
                    lazy = ann.fetch() == FetchType.LAZY;
                }
                // implicit type will check basic types and Serializable classes
                if (isId || (!optional && nullability != Nullability.FORCED_NULL)) {
                    // force columns to not null
                    for (Ejb3Column col : columns) {
                        if (isId && col.isFormula()) {
                            throw new CannotForceNonNullableException(String.format(Locale.ROOT, "Identifier property [%s] cannot contain formula mapping [%s]", HCANNHelper.annotatedElementSignature(property), col.getFormulaString()));
                        }
                        col.forceNotNull();
                    }
                }
                propertyBinder.setLazy(lazy);
                propertyBinder.setColumns(columns);
                if (isOverridden) {
                    final PropertyData mapsIdProperty = BinderHelper.getPropertyOverriddenByMapperOrMapsId(isId, propertyHolder, property.getName(), context);
                    propertyBinder.setReferencedEntityName(mapsIdProperty.getClassOrElementName());
                }
                propertyBinder.makePropertyValueAndBind();
            }
            if (isOverridden) {
                final PropertyData mapsIdProperty = BinderHelper.getPropertyOverriddenByMapperOrMapsId(isId, propertyHolder, property.getName(), context);
                final IdentifierGeneratorDefinition.Builder foreignGeneratorBuilder = new IdentifierGeneratorDefinition.Builder();
                foreignGeneratorBuilder.setName("Hibernate-local--foreign generator");
                foreignGeneratorBuilder.setStrategy("foreign");
                foreignGeneratorBuilder.addParam("property", mapsIdProperty.getPropertyName());
                final IdentifierGeneratorDefinition foreignGenerator = foreignGeneratorBuilder.build();
                // Map<String, IdentifierGeneratorDefinition> localGenerators = ( HashMap<String, IdentifierGeneratorDefinition> ) classGenerators.clone();
                // localGenerators.put( foreignGenerator.getName(), foreignGenerator );
                SecondPass secondPass = new IdGeneratorResolverSecondPass((SimpleValue) propertyBinder.getValue(), property, foreignGenerator.getStrategy(), foreignGenerator.getName(), context, foreignGenerator);
                context.getMetadataCollector().addSecondPass(secondPass);
            }
            if (isId) {
                // components and regular basic types create SimpleValue objects
                final SimpleValue value = (SimpleValue) propertyBinder.getValue();
                if (!isOverridden) {
                    processId(propertyHolder, inferredData, value, classGenerators, isIdentifierMapper, context);
                }
            }
        }
    }
    // init index
    // process indexes after everything: in second pass, many to one has to be done before indexes
    Index index = property.getAnnotation(Index.class);
    if (index != null) {
        if (joinColumns != null) {
            for (Ejb3Column column : joinColumns) {
                column.addIndex(index, inSecondPass);
            }
        } else {
            if (columns != null) {
                for (Ejb3Column column : columns) {
                    column.addIndex(index, inSecondPass);
                }
            }
        }
    }
    // Natural ID columns must reside in one single UniqueKey within the Table.
    // For now, simply ensure consistent naming.
    // TODO: AFAIK, there really isn't a reason for these UKs to be created
    // on the secondPass.  This whole area should go away...
    NaturalId naturalIdAnn = property.getAnnotation(NaturalId.class);
    if (naturalIdAnn != null) {
        if (joinColumns != null) {
            for (Ejb3Column column : joinColumns) {
                String keyName = "UK_" + Constraint.hashedName(column.getTable().getName() + "_NaturalID");
                column.addUniqueKey(keyName, inSecondPass);
            }
        } else {
            for (Ejb3Column column : columns) {
                String keyName = "UK_" + Constraint.hashedName(column.getTable().getName() + "_NaturalID");
                column.addUniqueKey(keyName, inSecondPass);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MapKeyJoinColumnDelegator(org.hibernate.cfg.annotations.MapKeyJoinColumnDelegator) Index(org.hibernate.annotations.Index) XClass(org.hibernate.annotations.common.reflection.XClass) ManyToOne(javax.persistence.ManyToOne) DiscriminatorFormula(org.hibernate.annotations.DiscriminatorFormula) Formula(org.hibernate.annotations.Formula) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) AnnotationException(org.hibernate.AnnotationException) ElementCollection(javax.persistence.ElementCollection) NaturalId(org.hibernate.annotations.NaturalId) RootClass(org.hibernate.mapping.RootClass) OrderColumn(javax.persistence.OrderColumn) EmbeddedId(javax.persistence.EmbeddedId) ListIndexBase(org.hibernate.annotations.ListIndexBase) OneToMany(javax.persistence.OneToMany) CollectionId(org.hibernate.annotations.CollectionId) LazyGroup(org.hibernate.annotations.LazyGroup) MapsId(javax.persistence.MapsId) NaturalId(org.hibernate.annotations.NaturalId) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id) CollectionId(org.hibernate.annotations.CollectionId) CollectionBinder(org.hibernate.cfg.annotations.CollectionBinder) OnDelete(org.hibernate.annotations.OnDelete) Cache(org.hibernate.annotations.Cache) Basic(javax.persistence.Basic) MapsId(javax.persistence.MapsId) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) OneToOne(javax.persistence.OneToOne) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) Cascade(org.hibernate.annotations.Cascade) XProperty(org.hibernate.annotations.common.reflection.XProperty) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) Columns(org.hibernate.annotations.Columns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Join(org.hibernate.mapping.Join) ManyToMany(javax.persistence.ManyToMany) MapKeyColumnDelegator(org.hibernate.cfg.annotations.MapKeyColumnDelegator) UniqueConstraint(javax.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint) SimpleValue(org.hibernate.mapping.SimpleValue) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) NotFound(org.hibernate.annotations.NotFound) JoinTable(javax.persistence.JoinTable)

Example 2 with LazyGroup

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

the class OneToOneSecondPass method doSecondPass.

// TODO refactor this code, there is a lot of duplication in this method
public void doSecondPass(Map persistentClasses) throws MappingException {
    org.hibernate.mapping.OneToOne value = new org.hibernate.mapping.OneToOne(buildingContext, propertyHolder.getTable(), propertyHolder.getPersistentClass());
    final String propertyName = inferredData.getPropertyName();
    value.setPropertyName(propertyName);
    String referencedEntityName = ToOneBinder.getReferenceEntityName(inferredData, targetEntity, buildingContext);
    value.setReferencedEntityName(referencedEntityName);
    AnnotationBinder.defineFetchingStrategy(value, inferredData.getProperty());
    // value.setFetchMode( fetchMode );
    value.setCascadeDeleteEnabled(cascadeOnDelete);
    if (!optional) {
        value.setConstrained(true);
    }
    if (value.isReferenceToPrimaryKey()) {
        value.setForeignKeyType(ForeignKeyDirection.TO_PARENT);
    } else {
        value.setForeignKeyType(value.isConstrained() ? ForeignKeyDirection.FROM_PARENT : ForeignKeyDirection.TO_PARENT);
    }
    PropertyBinder binder = new PropertyBinder();
    binder.setName(propertyName);
    binder.setValue(value);
    binder.setCascade(cascadeStrategy);
    binder.setAccessType(inferredData.getDefaultAccess());
    final LazyGroup lazyGroupAnnotation = inferredData.getProperty().getAnnotation(LazyGroup.class);
    if (lazyGroupAnnotation != null) {
        binder.setLazyGroup(lazyGroupAnnotation.value());
    }
    Property prop = binder.makeProperty();
    prop.setOptional(optional);
    if (BinderHelper.isEmptyAnnotationValue(mappedBy)) {
        /*
			 * we need to check if the columns are in the right order
			 * if not, then we need to create a many to one and formula
			 * but actually, since entities linked by a one to one need
			 * to share the same composite id class, this cannot happen in hibernate
			 */
        boolean rightOrder = true;
        if (rightOrder) {
            String path = StringHelper.qualify(propertyHolder.getPath(), propertyName);
            final ToOneFkSecondPass secondPass = new ToOneFkSecondPass(value, joinColumns, // cannot have nullabe and unique on certain DBs
            !optional, propertyHolder.getEntityOwnerClassName(), path, buildingContext);
            secondPass.doSecondPass(persistentClasses);
            // no column associated since its a one to one
            propertyHolder.addProperty(prop, inferredData.getDeclaringClass());
        } else {
        // this is a many to one with Formula
        }
    } else {
        PersistentClass otherSide = (PersistentClass) persistentClasses.get(value.getReferencedEntityName());
        Property otherSideProperty;
        try {
            if (otherSide == null) {
                throw new MappingException("Unable to find entity: " + value.getReferencedEntityName());
            }
            otherSideProperty = BinderHelper.findPropertyByName(otherSide, mappedBy);
        } catch (MappingException e) {
            throw new AnnotationException("Unknown mappedBy in: " + StringHelper.qualify(ownerEntity, ownerProperty) + ", referenced property unknown: " + StringHelper.qualify(value.getReferencedEntityName(), mappedBy));
        }
        if (otherSideProperty == null) {
            throw new AnnotationException("Unknown mappedBy in: " + StringHelper.qualify(ownerEntity, ownerProperty) + ", referenced property unknown: " + StringHelper.qualify(value.getReferencedEntityName(), mappedBy));
        }
        if (otherSideProperty.getValue() instanceof OneToOne) {
            propertyHolder.addProperty(prop, inferredData.getDeclaringClass());
        } else if (otherSideProperty.getValue() instanceof ManyToOne) {
            Iterator it = otherSide.getJoinIterator();
            Join otherSideJoin = null;
            while (it.hasNext()) {
                Join otherSideJoinValue = (Join) it.next();
                if (otherSideJoinValue.containsProperty(otherSideProperty)) {
                    otherSideJoin = otherSideJoinValue;
                    break;
                }
            }
            if (otherSideJoin != null) {
                // @OneToOne @JoinTable
                Join mappedByJoin = buildJoinFromMappedBySide((PersistentClass) persistentClasses.get(ownerEntity), otherSideProperty, otherSideJoin);
                ManyToOne manyToOne = new ManyToOne(buildingContext, mappedByJoin.getTable());
                // FIXME use ignore not found here
                manyToOne.setIgnoreNotFound(ignoreNotFound);
                manyToOne.setCascadeDeleteEnabled(value.isCascadeDeleteEnabled());
                manyToOne.setFetchMode(value.getFetchMode());
                manyToOne.setLazy(value.isLazy());
                manyToOne.setReferencedEntityName(value.getReferencedEntityName());
                manyToOne.setUnwrapProxy(value.isUnwrapProxy());
                prop.setValue(manyToOne);
                Iterator otherSideJoinKeyColumns = otherSideJoin.getKey().getColumnIterator();
                while (otherSideJoinKeyColumns.hasNext()) {
                    Column column = (Column) otherSideJoinKeyColumns.next();
                    Column copy = new Column();
                    copy.setLength(column.getLength());
                    copy.setScale(column.getScale());
                    copy.setValue(manyToOne);
                    copy.setName(column.getQuotedName());
                    copy.setNullable(column.isNullable());
                    copy.setPrecision(column.getPrecision());
                    copy.setUnique(column.isUnique());
                    copy.setSqlType(column.getSqlType());
                    copy.setCheckConstraint(column.getCheckConstraint());
                    copy.setComment(column.getComment());
                    copy.setDefaultValue(column.getDefaultValue());
                    manyToOne.addColumn(copy);
                }
                mappedByJoin.addProperty(prop);
            } else {
                propertyHolder.addProperty(prop, inferredData.getDeclaringClass());
            }
            value.setReferencedPropertyName(mappedBy);
            // HHH-6813
            // Foo: @Id long id, @OneToOne(mappedBy="foo") Bar bar
            // Bar: @Id @OneToOne Foo foo
            boolean referencesDerivedId = false;
            try {
                referencesDerivedId = otherSide.getIdentifier() instanceof Component && ((Component) otherSide.getIdentifier()).getProperty(mappedBy) != null;
            } catch (MappingException e) {
            // ignore
            }
            boolean referenceToPrimaryKey = referencesDerivedId || mappedBy == null;
            value.setReferenceToPrimaryKey(referenceToPrimaryKey);
            // loop of attempts to resolve identifiers.
            if (referencesDerivedId) {
                ((ManyToOne) otherSideProperty.getValue()).setReferenceToPrimaryKey(false);
            }
            String propertyRef = value.getReferencedPropertyName();
            if (propertyRef != null) {
                buildingContext.getMetadataCollector().addUniquePropertyReference(value.getReferencedEntityName(), propertyRef);
            }
        } else {
            throw new AnnotationException("Referenced property not a (One|Many)ToOne: " + StringHelper.qualify(otherSide.getEntityName(), mappedBy) + " in mappedBy of " + StringHelper.qualify(ownerEntity, ownerProperty));
        }
    }
    final ForeignKey fk = inferredData.getProperty().getAnnotation(ForeignKey.class);
    if (fk != null && !BinderHelper.isEmptyAnnotationValue(fk.name())) {
        value.setForeignKeyName(fk.name());
    } else {
        final javax.persistence.ForeignKey jpaFk = inferredData.getProperty().getAnnotation(javax.persistence.ForeignKey.class);
        if (jpaFk != null) {
            if (jpaFk.value() == ConstraintMode.NO_CONSTRAINT) {
                value.setForeignKeyName("none");
            } else {
                value.setForeignKeyName(StringHelper.nullIfEmpty(jpaFk.name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(jpaFk.foreignKeyDefinition()));
            }
        }
    }
}
Also used : OneToOne(org.hibernate.mapping.OneToOne) Join(org.hibernate.mapping.Join) ForeignKey(org.hibernate.annotations.ForeignKey) ManyToOne(org.hibernate.mapping.ManyToOne) MappingException(org.hibernate.MappingException) OneToOne(org.hibernate.mapping.OneToOne) Column(org.hibernate.mapping.Column) LazyGroup(org.hibernate.annotations.LazyGroup) Iterator(java.util.Iterator) AnnotationException(org.hibernate.AnnotationException) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with LazyGroup

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

the class CollectionBinder method bind.

public void bind() {
    this.collection = createCollection(propertyHolder.getPersistentClass());
    String role = StringHelper.qualify(propertyHolder.getPath(), propertyName);
    LOG.debugf("Collection role: %s", role);
    collection.setRole(role);
    collection.setMappedByProperty(mappedBy);
    if (property.isAnnotationPresent(MapKeyColumn.class) && mapKeyPropertyName != null) {
        throw new AnnotationException("Cannot mix @javax.persistence.MapKey and @MapKeyColumn or @org.hibernate.annotations.MapKey " + "on the same collection: " + StringHelper.qualify(propertyHolder.getPath(), propertyName));
    }
    // set explicit type information
    if (explicitType != null) {
        final TypeDefinition typeDef = buildingContext.getMetadataCollector().getTypeDefinition(explicitType);
        if (typeDef == null) {
            collection.setTypeName(explicitType);
            collection.setTypeParameters(explicitTypeParameters);
        } else {
            collection.setTypeName(typeDef.getTypeImplementorClass().getName());
            collection.setTypeParameters(typeDef.getParameters());
        }
    }
    // set laziness
    defineFetchingStrategy();
    collection.setBatchSize(batchSize);
    collection.setMutable(!property.isAnnotationPresent(Immutable.class));
    // work on association
    boolean isMappedBy = !BinderHelper.isEmptyAnnotationValue(mappedBy);
    final OptimisticLock lockAnn = property.getAnnotation(OptimisticLock.class);
    final boolean includeInOptimisticLockChecks = (lockAnn != null) ? !lockAnn.excluded() : !isMappedBy;
    collection.setOptimisticLocked(includeInOptimisticLockChecks);
    Persister persisterAnn = property.getAnnotation(Persister.class);
    if (persisterAnn != null) {
        collection.setCollectionPersisterClass(persisterAnn.impl());
    }
    applySortingAndOrdering(collection);
    // set cache
    if (StringHelper.isNotEmpty(cacheConcurrencyStrategy)) {
        collection.setCacheConcurrencyStrategy(cacheConcurrencyStrategy);
        collection.setCacheRegionName(cacheRegionName);
    }
    // SQL overriding
    SQLInsert sqlInsert = property.getAnnotation(SQLInsert.class);
    SQLUpdate sqlUpdate = property.getAnnotation(SQLUpdate.class);
    SQLDelete sqlDelete = property.getAnnotation(SQLDelete.class);
    SQLDeleteAll sqlDeleteAll = property.getAnnotation(SQLDeleteAll.class);
    Loader loader = property.getAnnotation(Loader.class);
    if (sqlInsert != null) {
        collection.setCustomSQLInsert(sqlInsert.sql().trim(), sqlInsert.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlInsert.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlUpdate != null) {
        collection.setCustomSQLUpdate(sqlUpdate.sql(), sqlUpdate.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlUpdate.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDelete != null) {
        collection.setCustomSQLDelete(sqlDelete.sql(), sqlDelete.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDelete.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDeleteAll != null) {
        collection.setCustomSQLDeleteAll(sqlDeleteAll.sql(), sqlDeleteAll.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDeleteAll.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (loader != null) {
        collection.setLoaderName(loader.namedQuery());
    }
    if (isMappedBy && (property.isAnnotationPresent(JoinColumn.class) || property.isAnnotationPresent(JoinColumns.class) || propertyHolder.getJoinTable(property) != null)) {
        String message = "Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn: ";
        message += StringHelper.qualify(propertyHolder.getPath(), propertyName);
        throw new AnnotationException(message);
    }
    if (!isMappedBy && oneToMany && property.isAnnotationPresent(OnDelete.class) && !property.isAnnotationPresent(JoinColumn.class)) {
        String message = "Unidirectional one-to-many associations annotated with @OnDelete must define @JoinColumn: ";
        message += StringHelper.qualify(propertyHolder.getPath(), propertyName);
        throw new AnnotationException(message);
    }
    collection.setInverse(isMappedBy);
    // many to many may need some second pass informations
    if (!oneToMany && isMappedBy) {
        buildingContext.getMetadataCollector().addMappedBy(getCollectionType().getName(), mappedBy, propertyName);
    }
    // TODO reducce tableBinder != null and oneToMany
    XClass collectionType = getCollectionType();
    if (inheritanceStatePerClass == null)
        throw new AssertionFailure("inheritanceStatePerClass not set");
    SecondPass sp = getSecondPass(fkJoinColumns, joinColumns, inverseJoinColumns, elementColumns, mapKeyColumns, mapKeyManyToManyColumns, isEmbedded, property, collectionType, ignoreNotFound, oneToMany, tableBinder, buildingContext);
    if (collectionType.isAnnotationPresent(Embeddable.class) || // JPA 2
    property.isAnnotationPresent(ElementCollection.class)) {
        // do it right away, otherwise @ManyToOne on composite element call addSecondPass
        // and raise a ConcurrentModificationException
        // sp.doSecondPass( CollectionHelper.EMPTY_MAP );
        buildingContext.getMetadataCollector().addSecondPass(sp, !isMappedBy);
    } else {
        buildingContext.getMetadataCollector().addSecondPass(sp, !isMappedBy);
    }
    buildingContext.getMetadataCollector().addCollectionBinding(collection);
    // property building
    PropertyBinder binder = new PropertyBinder();
    binder.setName(propertyName);
    binder.setValue(collection);
    binder.setCascade(cascadeStrategy);
    if (cascadeStrategy != null && cascadeStrategy.contains("delete-orphan")) {
        collection.setOrphanDelete(true);
    }
    binder.setLazy(collection.isLazy());
    final LazyGroup lazyGroupAnnotation = property.getAnnotation(LazyGroup.class);
    if (lazyGroupAnnotation != null) {
        binder.setLazyGroup(lazyGroupAnnotation.value());
    }
    binder.setAccessType(accessType);
    binder.setProperty(property);
    binder.setInsertable(insertable);
    binder.setUpdatable(updatable);
    Property prop = binder.makeProperty();
    // we don't care about the join stuffs because the column is on the association table.
    if (!declaringClassSet)
        throw new AssertionFailure("DeclaringClass is not set in CollectionBinder while binding");
    propertyHolder.addProperty(prop, declaringClass);
}
Also used : AssertionFailure(org.hibernate.annotations.common.AssertionFailure) MapKeyColumn(javax.persistence.MapKeyColumn) Loader(org.hibernate.annotations.Loader) SQLDelete(org.hibernate.annotations.SQLDelete) XClass(org.hibernate.annotations.common.reflection.XClass) TypeDefinition(org.hibernate.boot.model.TypeDefinition) Embeddable(javax.persistence.Embeddable) SecondPass(org.hibernate.cfg.SecondPass) CollectionSecondPass(org.hibernate.cfg.CollectionSecondPass) LazyGroup(org.hibernate.annotations.LazyGroup) AnnotationException(org.hibernate.AnnotationException) SQLInsert(org.hibernate.annotations.SQLInsert) SQLUpdate(org.hibernate.annotations.SQLUpdate) SQLDeleteAll(org.hibernate.annotations.SQLDeleteAll) Persister(org.hibernate.annotations.Persister) ElementCollection(javax.persistence.ElementCollection) OptimisticLock(org.hibernate.annotations.OptimisticLock) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Aggregations

AnnotationException (org.hibernate.AnnotationException)3 LazyGroup (org.hibernate.annotations.LazyGroup)3 Property (org.hibernate.mapping.Property)3 ElementCollection (javax.persistence.ElementCollection)2 MapKeyColumn (javax.persistence.MapKeyColumn)2 PropertyBinder (org.hibernate.cfg.annotations.PropertyBinder)2 Join (org.hibernate.mapping.Join)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Basic (javax.persistence.Basic)1 Column (javax.persistence.Column)1 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)1 Embeddable (javax.persistence.Embeddable)1 EmbeddedId (javax.persistence.EmbeddedId)1 Id (javax.persistence.Id)1 JoinColumn (javax.persistence.JoinColumn)1 JoinColumns (javax.persistence.JoinColumns)1 JoinTable (javax.persistence.JoinTable)1 ManyToMany (javax.persistence.ManyToMany)1 ManyToOne (javax.persistence.ManyToOne)1