Search in sources :

Example 26 with CompositeType

use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.

the class AbstractCompositionAttribute method getAttributes.

@Override
public Iterable<AttributeDefinition> getAttributes() {
    return new Iterable<AttributeDefinition>() {

        @Override
        public Iterator<AttributeDefinition> iterator() {
            return new Iterator<AttributeDefinition>() {

                private final int numberOfAttributes = getType().getSubtypes().length;

                private int currentSubAttributeNumber;

                private int currentColumnPosition = columnStartPosition;

                @Override
                public boolean hasNext() {
                    return currentSubAttributeNumber < numberOfAttributes;
                }

                @Override
                public AttributeDefinition next() {
                    final int subAttributeNumber = currentSubAttributeNumber;
                    currentSubAttributeNumber++;
                    final String name = getType().getPropertyNames()[subAttributeNumber];
                    final Type type = getType().getSubtypes()[subAttributeNumber];
                    int columnPosition = currentColumnPosition;
                    currentColumnPosition += type.getColumnSpan(sessionFactory());
                    if (type.isAssociationType()) {
                        // we build the association-key here because of the "goofiness" with 'currentColumnPosition'
                        final AssociationKey associationKey;
                        final AssociationType aType = (AssociationType) type;
                        final Joinable joinable = aType.getAssociatedJoinable(sessionFactory());
                        if (aType.isAnyType()) {
                            associationKey = new AssociationKey(JoinHelper.getLHSTableName(aType, attributeNumber(), (OuterJoinLoadable) locateOwningPersister()), JoinHelper.getLHSColumnNames(aType, attributeNumber(), columnPosition, (OuterJoinLoadable) locateOwningPersister(), sessionFactory()));
                        } else if (aType.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
                            final String lhsTableName;
                            final String[] lhsColumnNames;
                            if (joinable.isCollection()) {
                                final QueryableCollection collectionPersister = (QueryableCollection) joinable;
                                lhsTableName = collectionPersister.getTableName();
                                lhsColumnNames = collectionPersister.getElementColumnNames();
                            } else {
                                final OuterJoinLoadable entityPersister = (OuterJoinLoadable) locateOwningPersister();
                                lhsTableName = getLHSTableName(aType, attributeNumber(), entityPersister);
                                lhsColumnNames = getLHSColumnNames(aType, attributeNumber(), columnPosition, entityPersister, sessionFactory());
                            }
                            associationKey = new AssociationKey(lhsTableName, lhsColumnNames);
                        } else {
                            associationKey = new AssociationKey(joinable.getTableName(), getRHSColumnNames(aType, sessionFactory()));
                        }
                        final CompositeType cType = getType();
                        final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                        return new CompositeBasedAssociationAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (AssociationType) type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation(), subAttributeNumber, associationKey);
                    } else if (type.isComponentType()) {
                        return new CompositionBasedCompositionAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (CompositeType) type, columnPosition, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(getType().getPropertyNullability()[subAttributeNumber]).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    } else {
                        final CompositeType cType = getType();
                        final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                        return new CompositeBasedBasicAttribute(AbstractCompositionAttribute.this, sessionFactory(), subAttributeNumber, name, type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove operation not supported here");
                }
            };
        }
    };
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) Iterator(java.util.Iterator) BaselineAttributeInformation(org.hibernate.tuple.BaselineAttributeInformation) CompositeType(org.hibernate.type.CompositeType)

Example 27 with CompositeType

use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.

the class AbstractEntityTuplizer method setIdentifier.

@Override
public void setIdentifier(Object entity, Serializable id, SharedSessionContractImplementor session) {
    if (entityMetamodel.getIdentifierProperty().isEmbedded()) {
        if (entity != id) {
            CompositeType copier = (CompositeType) entityMetamodel.getIdentifierProperty().getType();
            copier.setPropertyValues(entity, copier.getPropertyValues(id, getEntityMode()), getEntityMode());
        }
    } else if (idSetter != null) {
        idSetter.set(entity, id, getFactory());
    } else if (identifierMapperType != null) {
        mappedIdentifierValueMarshaller.setIdentifier(entity, id, getEntityMode(), session);
    }
}
Also used : CompositeType(org.hibernate.type.CompositeType)

Example 28 with CompositeType

use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.

the class ForeignKeys method collectNonNullableTransientEntities.

private static void collectNonNullableTransientEntities(Nullifier nullifier, Object value, String propertyName, Type type, boolean isNullable, SharedSessionContractImplementor session, NonNullableTransientDependencies nonNullableTransientEntities) {
    if (value == null) {
        return;
    }
    if (type.isEntityType()) {
        final EntityType entityType = (EntityType) type;
        if (!isNullable && !entityType.isOneToOne() && nullifier.isNullifiable(entityType.getAssociatedEntityName(), value)) {
            nonNullableTransientEntities.add(propertyName, value);
        }
    } else if (type.isAnyType()) {
        if (!isNullable && nullifier.isNullifiable(null, value)) {
            nonNullableTransientEntities.add(propertyName, value);
        }
    } else if (type.isComponentType()) {
        final CompositeType actype = (CompositeType) type;
        final boolean[] subValueNullability = actype.getPropertyNullability();
        if (subValueNullability != null) {
            final String[] subPropertyNames = actype.getPropertyNames();
            final Object[] subvalues = actype.getPropertyValues(value, session);
            final Type[] subtypes = actype.getSubtypes();
            for (int j = 0; j < subvalues.length; j++) {
                collectNonNullableTransientEntities(nullifier, subvalues[j], subPropertyNames[j], subtypes[j], subValueNullability[j], session, nonNullableTransientEntities);
            }
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CompositeType(org.hibernate.type.CompositeType)

Example 29 with CompositeType

use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.

the class Cascade method cascadeProperty.

/**
	 * Cascade an action to the child or children
	 */
private static void cascadeProperty(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final Type type, final CascadeStyle style, final String propertyName, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException {
    if (child != null) {
        if (type.isAssociationType()) {
            final AssociationType associationType = (AssociationType) type;
            if (cascadeAssociationNow(cascadePoint, associationType)) {
                cascadeAssociation(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, type, style, anything, isCascadeDeleteEnabled);
            }
        } else if (type.isComponentType()) {
            cascadeComponent(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, (CompositeType) type, anything);
        }
    }
    // potentially we need to handle orphan deletes for one-to-ones here...
    if (isLogicalOneToOne(type)) {
        // orphan checking
        if (style.hasOrphanDelete() && action.deleteOrphans()) {
            // value is orphaned if loaded state for this property shows not null
            // because it is currently null.
            final EntityEntry entry = eventSource.getPersistenceContext().getEntry(parent);
            if (entry != null && entry.getStatus() != Status.SAVING) {
                final Object loadedValue;
                if (componentPathStackDepth == 0) {
                    // association defined on entity
                    loadedValue = entry.getLoadedValue(propertyName);
                } else {
                    // association defined on component
                    // 		todo : this is currently unsupported because of the fact that
                    //		we do not know the loaded state of this value properly
                    //		and doing so would be very difficult given how components and
                    //		entities are loaded (and how 'loaded state' is put into the
                    //		EntityEntry).  Solutions here are to either:
                    //			1) properly account for components as a 2-phase load construct
                    //			2) just assume the association was just now orphaned and
                    // 				issue the orphan delete.  This would require a special
                    //				set of SQL statements though since we do not know the
                    //				orphaned value, something a delete with a subquery to
                    // 				match the owner.
                    //							final EntityType entityType = (EntityType) type;
                    //							final String getPropertyPath = composePropertyPath( entityType.getPropertyName() );
                    loadedValue = null;
                }
                // entity is managed (without first nulling and manually flushing).
                if (child == null || (loadedValue != null && child != loadedValue)) {
                    final EntityEntry valueEntry = eventSource.getPersistenceContext().getEntry(loadedValue);
                    // already been flushed.  See HHH-7829.
                    if (valueEntry != null) {
                        final String entityName = valueEntry.getPersister().getEntityName();
                        if (LOG.isTraceEnabled()) {
                            final Serializable id = valueEntry.getPersister().getIdentifier(loadedValue, eventSource);
                            final String description = MessageHelper.infoString(entityName, id);
                            LOG.tracev("Deleting orphaned entity instance: {0}", description);
                        }
                        if (type.isAssociationType() && ((AssociationType) type).getForeignKeyDirection().equals(ForeignKeyDirection.TO_PARENT)) {
                            // If FK direction is to-parent, we must remove the orphan *beforeQuery* the queued update(s)
                            // occur.  Otherwise, replacing the association on a managed entity, without manually
                            // nulling and flushing, causes FK constraint violations.
                            eventSource.removeOrphanBeforeUpdates(entityName, loadedValue);
                        } else {
                            // Else, we must delete afterQuery the updates.
                            eventSource.delete(entityName, loadedValue, isCascadeDeleteEnabled, new HashSet());
                        }
                    }
                }
            }
        }
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) AssociationType(org.hibernate.type.AssociationType) CompositeType(org.hibernate.type.CompositeType) HashSet(java.util.HashSet)

Example 30 with CompositeType

use of org.hibernate.type.CompositeType in project hibernate-orm by hibernate.

the class Cascade method cascadeComponent.

private static void cascadeComponent(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final CompositeType componentType, final Object anything) {
    Object[] children = null;
    final Type[] types = componentType.getSubtypes();
    final String[] propertyNames = componentType.getPropertyNames();
    for (int i = 0; i < types.length; i++) {
        final CascadeStyle componentPropertyStyle = componentType.getCascadeStyle(i);
        final String subPropertyName = propertyNames[i];
        if (componentPropertyStyle.doCascade(action)) {
            if (children == null) {
                // Get children on demand.
                children = componentType.getPropertyValues(child, eventSource);
            }
            cascadeProperty(action, cascadePoint, eventSource, componentPathStackDepth + 1, parent, children[i], types[i], componentPropertyStyle, subPropertyName, anything, false);
        }
    }
}
Also used : CascadeStyle(org.hibernate.engine.spi.CascadeStyle) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type)

Aggregations

CompositeType (org.hibernate.type.CompositeType)32 Type (org.hibernate.type.Type)22 AssociationType (org.hibernate.type.AssociationType)12 EntityType (org.hibernate.type.EntityType)11 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 MappingException (org.hibernate.MappingException)3 QueryException (org.hibernate.QueryException)3 ExpandingCompositeQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingCompositeQuerySpace)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 HashSet (java.util.HashSet)2 HibernateException (org.hibernate.HibernateException)2 TypedValue (org.hibernate.engine.spi.TypedValue)2 CompositePropertyMapping (org.hibernate.loader.plan.build.internal.spaces.CompositePropertyMapping)2 ExpandingEntityQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingEntityQuerySpace)2 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)2 AttributeDefinition (org.hibernate.persister.walking.spi.AttributeDefinition)2