Search in sources :

Example 1 with MappingType

use of org.hibernate.metamodel.mapping.MappingType in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method checkIdClass.

private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadType loadType, final Class<?> idClass) {
    // we may have the jpa requirement of allowing find-by-id where id is the "simple pk value" of a
    // dependent objects parent.  This is part of its generally goofy derived identity "feature"
    final EntityIdentifierMapping idMapping = persister.getIdentifierMapping();
    if (idMapping instanceof CompositeIdentifierMapping) {
        final CompositeIdentifierMapping compositeIdMapping = (CompositeIdentifierMapping) idMapping;
        final List<AttributeMapping> attributeMappings = compositeIdMapping.getPartMappingType().getAttributeMappings();
        if (attributeMappings.size() == 1) {
            final AttributeMapping singleIdAttribute = attributeMappings.get(0);
            if (singleIdAttribute.getMappedType() instanceof EntityMappingType) {
                final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
                final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
                final MappingType parentIdType = parentIdTargetIdMapping instanceof CompositeIdentifierMapping ? ((CompositeIdentifierMapping) parentIdTargetIdMapping).getMappedIdEmbeddableTypeDescriptor() : parentIdTargetIdMapping.getMappedType();
                if (parentIdType.getMappedJavaType().getJavaTypeClass().isInstance(event.getEntityId())) {
                    // yep that's what we have...
                    loadByDerivedIdentitySimplePkValue(event, loadType, persister, compositeIdMapping, (EntityPersister) parentIdTargetMapping);
                    return;
                }
            }
        } else if (idMapping instanceof NonAggregatedIdentifierMapping) {
            if (idClass.isInstance(event.getEntityId())) {
                return;
            }
        }
    }
    throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
Also used : EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TypeMismatchException(org.hibernate.TypeMismatchException) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 2 with MappingType

use of org.hibernate.metamodel.mapping.MappingType in project hibernate-orm by hibernate.

the class ColumnReference method getTableReference.

TableReference getTableReference(TableGroup tableGroup) {
    ModelPartContainer modelPart = tableGroup.getModelPart();
    if (modelPart instanceof PluralAttributeMapping) {
        final PluralAttributeMapping pluralAttribute = (PluralAttributeMapping) modelPart;
        if (!pluralAttribute.getCollectionDescriptor().hasManyToManyOrdering()) {
            return tableGroup.getPrimaryTableReference();
        }
        final MappingType elementMappingType = pluralAttribute.getElementDescriptor().getPartMappingType();
        if (elementMappingType instanceof AbstractEntityPersister) {
            final AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) elementMappingType;
            final int tableNumber = abstractEntityPersister.determineTableNumberForColumn(columnExpression);
            final String tableName = abstractEntityPersister.getTableName(tableNumber);
            return tableGroup.getTableReference(tableGroup.getNavigablePath(), tableName);
        } else {
            return tableGroup.getPrimaryTableReference();
        }
    }
    return null;
}
Also used : MappingType(org.hibernate.metamodel.mapping.MappingType) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister)

Aggregations

MappingType (org.hibernate.metamodel.mapping.MappingType)2 TypeMismatchException (org.hibernate.TypeMismatchException)1 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 CompositeIdentifierMapping (org.hibernate.metamodel.mapping.CompositeIdentifierMapping)1 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 ModelPartContainer (org.hibernate.metamodel.mapping.ModelPartContainer)1 NonAggregatedIdentifierMapping (org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1