Search in sources :

Example 41 with AssociationType

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

the class AbstractPropertyMapping method initPropertyPaths.

/*protected void initPropertyPaths(
			final String path,
			final Type type,
			final String[] columns,
			final String[] formulaTemplates,
			final Mapping factory)
	throws MappingException {
		//addFormulaPropertyPath(path, type, formulaTemplates);
		initPropertyPaths(path, type, columns, formulaTemplates, factory);
	}*/
protected void initPropertyPaths(final String path, final Type type, String[] columns, String[] columnReaders, String[] columnReaderTemplates, final String[] formulaTemplates, final Mapping factory) throws MappingException {
    assert columns != null : "Incoming columns should not be null : " + path;
    assert type != null : "Incoming type should not be null : " + path;
    if (columns.length != type.getColumnSpan(factory)) {
        throw new MappingException("broken column mapping for: " + path + " of: " + getEntityName());
    }
    if (type.isAssociationType()) {
        AssociationType actype = (AssociationType) type;
        if (actype.useLHSPrimaryKey()) {
            columns = getIdentifierColumnNames();
            columnReaders = getIdentifierColumnReaders();
            columnReaderTemplates = getIdentifierColumnReaderTemplates();
        } else {
            String foreignKeyProperty = actype.getLHSPropertyName();
            if (foreignKeyProperty != null && !path.equals(foreignKeyProperty)) {
                //TODO: this requires that the collection is defined afterQuery the
                //      referenced property in the mapping file (ok?)
                columns = columnsByPropertyPath.get(foreignKeyProperty);
                if (columns == null) {
                    //get em on the second pass!
                    return;
                }
                columnReaders = columnReadersByPropertyPath.get(foreignKeyProperty);
                columnReaderTemplates = columnReaderTemplatesByPropertyPath.get(foreignKeyProperty);
            }
        }
    }
    if (path != null) {
        addPropertyPath(path, type, columns, columnReaders, columnReaderTemplates, formulaTemplates);
    }
    if (type.isComponentType()) {
        CompositeType actype = (CompositeType) type;
        initComponentPropertyPaths(path, actype, columns, columnReaders, columnReaderTemplates, formulaTemplates, factory);
        if (actype.isEmbedded()) {
            initComponentPropertyPaths(path == null ? null : StringHelper.qualifier(path), actype, columns, columnReaders, columnReaderTemplates, formulaTemplates, factory);
        }
    } else if (type.isEntityType()) {
        initIdentifierPropertyPaths(path, (EntityType) type, columns, columnReaders, columnReaderTemplates, factory);
    }
}
Also used : EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) MappingException(org.hibernate.MappingException) CompositeType(org.hibernate.type.CompositeType)

Example 42 with AssociationType

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

the class SingleTableEntityPersister method getSubclassPropertyTableNumber.

private int getSubclassPropertyTableNumber(String propertyName, String entityName) {
    // When there are duplicated property names in the subclasses
    // then propertyMapping.toType( propertyName ) may return an
    // incorrect Type. To ensure correct results, lookup the property type
    // using the concrete EntityPersister with the specified entityName
    // (since the concrete EntityPersister cannot have duplicated property names).
    final EntityPersister concreteEntityPersister;
    if (getEntityName().equals(entityName)) {
        concreteEntityPersister = this;
    } else {
        concreteEntityPersister = getFactory().getMetamodel().entityPersister(entityName);
    }
    Type type = concreteEntityPersister.getPropertyType(propertyName);
    if (type.isAssociationType() && ((AssociationType) type).useLHSPrimaryKey()) {
        return 0;
    }
    final Integer tabnum = propertyTableNumbersByNameAndSubclass.get(entityName + '.' + propertyName);
    return tabnum == null ? 0 : tabnum;
}
Also used : DiscriminatorType(org.hibernate.type.DiscriminatorType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type)

Example 43 with AssociationType

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

the class CompositionSingularSubAttributesHelper method getSingularSubAttributes.

private static Iterable<AttributeDefinition> getSingularSubAttributes(final AttributeSource source, final OuterJoinLoadable ownerEntityPersister, final CompositeType compositeType, final String lhsTableName, final String[] lhsColumns) {
    return new Iterable<AttributeDefinition>() {

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

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

                private int currentSubAttributeNumber;

                private int currentColumnPosition;

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

                @Override
                public AttributeDefinition next() {
                    final int subAttributeNumber = currentSubAttributeNumber;
                    currentSubAttributeNumber++;
                    final String name = compositeType.getPropertyNames()[subAttributeNumber];
                    final Type type = compositeType.getSubtypes()[subAttributeNumber];
                    final int columnPosition = currentColumnPosition;
                    final int columnSpan = type.getColumnSpan(ownerEntityPersister.getFactory());
                    final String[] subAttributeLhsColumns = ArrayHelper.slice(lhsColumns, columnPosition, columnSpan);
                    final boolean[] propertyNullability = compositeType.getPropertyNullability();
                    final boolean nullable = propertyNullability == null || propertyNullability[subAttributeNumber];
                    currentColumnPosition += columnSpan;
                    if (type.isAssociationType()) {
                        final AssociationType aType = (AssociationType) type;
                        return new AssociationAttributeDefinition() {

                            @Override
                            public AssociationKey getAssociationKey() {
                                return new AssociationKey(lhsTableName, subAttributeLhsColumns);
                            }

                            @Override
                            public AssociationNature getAssociationNature() {
                                if (type.isAnyType()) {
                                    return AssociationNature.ANY;
                                } else {
                                    // cannot be a collection
                                    return AssociationNature.ENTITY;
                                }
                            }

                            @Override
                            public EntityDefinition toEntityDefinition() {
                                if (getAssociationNature() != AssociationNature.ENTITY) {
                                    throw new WalkingException("Cannot build EntityDefinition from non-entity-typed attribute");
                                }
                                return (EntityPersister) aType.getAssociatedJoinable(ownerEntityPersister.getFactory());
                            }

                            @Override
                            public AnyMappingDefinition toAnyDefinition() {
                                if (getAssociationNature() != AssociationNature.ANY) {
                                    throw new WalkingException("Cannot build AnyMappingDefinition from non-any-typed attribute");
                                }
                                // todo : not sure how lazy is propogated into the component for a subattribute of type any
                                return new StandardAnyTypeDefinition((AnyType) aType, false);
                            }

                            @Override
                            public CollectionDefinition toCollectionDefinition() {
                                throw new WalkingException("A collection cannot be mapped to a composite ID sub-attribute.");
                            }

                            @Override
                            public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
                                return new FetchStrategy(FetchTiming.IMMEDIATE, FetchStyle.JOIN);
                            }

                            @Override
                            public CascadeStyle determineCascadeStyle() {
                                return CascadeStyles.NONE;
                            }

                            @Override
                            public HydratedCompoundValueHandler getHydratedCompoundValueExtractor() {
                                return null;
                            }

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public AssociationType getType() {
                                return aType;
                            }

                            @Override
                            public boolean isNullable() {
                                return nullable;
                            }

                            @Override
                            public AttributeSource getSource() {
                                return source;
                            }
                        };
                    } else if (type.isComponentType()) {
                        return new CompositionDefinition() {

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public CompositeType getType() {
                                return (CompositeType) type;
                            }

                            @Override
                            public boolean isNullable() {
                                return nullable;
                            }

                            @Override
                            public AttributeSource getSource() {
                                return source;
                            }

                            @Override
                            public Iterable<AttributeDefinition> getAttributes() {
                                return CompositionSingularSubAttributesHelper.getSingularSubAttributes(this, ownerEntityPersister, (CompositeType) type, lhsTableName, subAttributeLhsColumns);
                            }
                        };
                    } else {
                        return new AttributeDefinition() {

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public Type getType() {
                                return type;
                            }

                            @Override
                            public boolean isNullable() {
                                return nullable;
                            }

                            @Override
                            public AttributeSource getSource() {
                                return source;
                            }
                        };
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove operation not supported here");
                }
            };
        }
    };
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) AttributeSource(org.hibernate.persister.walking.spi.AttributeSource) FetchStrategy(org.hibernate.engine.FetchStrategy) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition) AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) WalkingException(org.hibernate.persister.walking.spi.WalkingException) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) AnyType(org.hibernate.type.AnyType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) Iterator(java.util.Iterator) PropertyPath(org.hibernate.loader.PropertyPath) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition) CompositionDefinition(org.hibernate.persister.walking.spi.CompositionDefinition) CompositeType(org.hibernate.type.CompositeType)

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