Search in sources :

Example 26 with AssociationType

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

the class CriteriaQueryTranslator method getPathInfo.

private CriteriaInfoProvider getPathInfo(String path) {
    StringTokenizer tokens = new StringTokenizer(path, ".");
    String componentPath = "";
    // start with the 'rootProvider'
    CriteriaInfoProvider provider = nameCriteriaInfoMap.get(rootEntityName);
    while (tokens.hasMoreTokens()) {
        componentPath += tokens.nextToken();
        final Type type = provider.getType(componentPath);
        if (type.isAssociationType()) {
            // CollectionTypes are always also AssociationTypes - but there's not always an associated entity...
            final AssociationType atype = (AssociationType) type;
            final CollectionType ctype = type.isCollectionType() ? (CollectionType) type : null;
            final Type elementType = (ctype != null) ? ctype.getElementType(sessionFactory) : null;
            // is the association a collection of components or value-types? (i.e a colloction of valued types?)
            if (ctype != null && elementType.isComponentType()) {
                provider = new ComponentCollectionCriteriaInfoProvider(helper.getCollectionPersister(ctype.getRole()));
            } else if (ctype != null && !elementType.isEntityType()) {
                provider = new ScalarCollectionCriteriaInfoProvider(helper, ctype.getRole());
            } else {
                provider = new EntityCriteriaInfoProvider((Queryable) sessionFactory.getEntityPersister(atype.getAssociatedEntityName(sessionFactory)));
            }
            componentPath = "";
        } else if (type.isComponentType()) {
            if (!tokens.hasMoreTokens()) {
                throw new QueryException("Criteria objects cannot be created directly on components.  Create a criteria on " + "owning entity and use a dotted property to access component property: " + path);
            } else {
                componentPath += '.';
            }
        } else {
            throw new QueryException("not an association: " + componentPath);
        }
    }
    return provider;
}
Also used : StringTokenizer(java.util.StringTokenizer) StringRepresentableType(org.hibernate.type.StringRepresentableType) CollectionType(org.hibernate.type.CollectionType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) AssociationType(org.hibernate.type.AssociationType) CollectionType(org.hibernate.type.CollectionType)

Example 27 with AssociationType

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

the class PropertyFactory method buildStandardProperty.

/**
	 * @deprecated See mainly {@link #buildEntityBasedAttribute}
	 */
@Deprecated
public static StandardProperty buildStandardProperty(Property property, boolean lazyAvailable) {
    final Type type = property.getValue().getType();
    // we need to dirty check collections, since they can cause an owner
    // version number increment
    // we need to dirty check many-to-ones with not-found="ignore" in order
    // to update the cache (not the database), since in this case a null
    // entity reference can lose information
    boolean alwaysDirtyCheck = type.isAssociationType() && ((AssociationType) type).isAlwaysDirtyChecked();
    return new StandardProperty(property.getName(), type, lazyAvailable && property.isLazy(), property.isInsertable(), property.isUpdateable(), property.getValueGenerationStrategy(), property.isOptional(), alwaysDirtyCheck || property.isUpdateable(), property.isOptimisticLocked(), property.getCascadeStyle(), property.getValue().getFetchMode());
}
Also used : CompositeType(org.hibernate.type.CompositeType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type)

Example 28 with AssociationType

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

the class PropertyFactory method buildEntityBasedAttribute.

/**
	 * Generate a non-identifier (and non-version) attribute based on the given mapped property from the given entity
	 *
	 * @param property The mapped property.
	 * @param lazyAvailable Is property lazy loading currently available.
	 *
	 * @return The appropriate NonIdentifierProperty definition.
	 */
public static NonIdentifierAttribute buildEntityBasedAttribute(EntityPersister persister, SessionFactoryImplementor sessionFactory, int attributeNumber, Property property, boolean lazyAvailable) {
    final Type type = property.getValue().getType();
    final NonIdentifierAttributeNature nature = decode(type);
    // we need to dirty check collections, since they can cause an owner
    // version number increment
    // we need to dirty check many-to-ones with not-found="ignore" in order 
    // to update the cache (not the database), since in this case a null
    // entity reference can lose information
    boolean alwaysDirtyCheck = type.isAssociationType() && ((AssociationType) type).isAlwaysDirtyChecked();
    switch(nature) {
        case BASIC:
            {
                return new EntityBasedBasicAttribute(persister, sessionFactory, attributeNumber, property.getName(), type, new BaselineAttributeInformation.Builder().setLazy(lazyAvailable && property.isLazy()).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(alwaysDirtyCheck || property.isUpdateable()).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).setFetchMode(property.getValue().getFetchMode()).createInformation());
            }
        case COMPOSITE:
            {
                return new EntityBasedCompositionAttribute(persister, sessionFactory, attributeNumber, property.getName(), (CompositeType) type, new BaselineAttributeInformation.Builder().setLazy(lazyAvailable && property.isLazy()).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(alwaysDirtyCheck || property.isUpdateable()).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).setFetchMode(property.getValue().getFetchMode()).createInformation());
            }
        case ENTITY:
        case ANY:
        case COLLECTION:
            {
                return new EntityBasedAssociationAttribute(persister, sessionFactory, attributeNumber, property.getName(), (AssociationType) type, new BaselineAttributeInformation.Builder().setLazy(lazyAvailable && property.isLazy()).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(alwaysDirtyCheck || property.isUpdateable()).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).setFetchMode(property.getValue().getFetchMode()).createInformation());
            }
        default:
            {
                throw new HibernateException("Internal error");
            }
    }
}
Also used : CompositeType(org.hibernate.type.CompositeType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) EntityBasedAssociationAttribute(org.hibernate.tuple.entity.EntityBasedAssociationAttribute) AssociationType(org.hibernate.type.AssociationType) EntityBasedCompositionAttribute(org.hibernate.tuple.entity.EntityBasedCompositionAttribute) HibernateException(org.hibernate.HibernateException) EntityBasedBasicAttribute(org.hibernate.tuple.entity.EntityBasedBasicAttribute) CompositeType(org.hibernate.type.CompositeType)

Example 29 with AssociationType

use of org.hibernate.type.AssociationType 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 30 with AssociationType

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

the class EntityBasedAssociationAttribute method getAssociationKey.

@Override
public AssociationKey getAssociationKey() {
    final AssociationType type = getType();
    if (type.isAnyType()) {
        return new AssociationKey(JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()), JoinHelper.getLHSColumnNames(type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
    }
    final Joinable joinable = type.getAssociatedJoinable(sessionFactory());
    if (type.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) source();
            lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
            lhsColumnNames = getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
        }
        return new AssociationKey(lhsTableName, lhsColumnNames);
    } else {
        return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
    }
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Aggregations

AssociationType (org.hibernate.type.AssociationType)43 Type (org.hibernate.type.Type)18 FetchStyle (org.hibernate.engine.FetchStyle)17 FetchTiming (org.hibernate.engine.FetchTiming)17 Test (org.junit.Test)17 CompositeType (org.hibernate.type.CompositeType)12 EntityType (org.hibernate.type.EntityType)10 JoinType (org.hibernate.sql.JoinType)9 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)5 UniqueKeyLoadable (org.hibernate.persister.entity.UniqueKeyLoadable)4 CollectionType (org.hibernate.type.CollectionType)4 VersionType (org.hibernate.type.VersionType)4 Iterator (java.util.Iterator)3 ManagedType (javax.persistence.metamodel.ManagedType)3 QueryException (org.hibernate.QueryException)3 Joinable (org.hibernate.persister.entity.Joinable)3 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)3 Serializable (java.io.Serializable)2