Search in sources :

Example 1 with UnsupportedMappingException

use of org.hibernate.metamodel.UnsupportedMappingException in project hibernate-orm by hibernate.

the class AttributeFactory method determineAttributeMetadata.

private static <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver, MetadataContext context) {
    final Property propertyMapping = attributeContext.getPropertyMapping();
    final String propertyName = propertyMapping.getName();
    LOG.tracef("Starting attribute metadata determination [%s]", propertyName);
    final Member member = memberResolver.resolveMember(attributeContext, context);
    LOG.tracef("    Determined member [%s]", member);
    final Value value = propertyMapping.getValue();
    final org.hibernate.type.Type type = value.getType();
    LOG.tracef("    Determined type [name=%s, class=%s]", type.getName(), type.getClass().getName());
    if (type.isAnyType()) {
        return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.ANY, context);
    } else if (type.isAssociationType()) {
        // collection or entity
        if (type.isEntityType()) {
            // entity
            return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, determineSingularAssociationClassification(member), context);
        }
        // collection
        if (value instanceof Collection) {
            final Collection collValue = (Collection) value;
            final Value elementValue = collValue.getElement();
            final org.hibernate.type.Type elementType = elementValue.getType();
            final boolean isManyToMany = isManyToMany(member);
            // First, determine the type of the elements and use that to help determine the
            // collection type
            final AttributeClassification elementClassification;
            final AttributeClassification attributeClassification;
            if (elementType.isAnyType()) {
                attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
                elementClassification = AttributeClassification.ANY;
            } else if (elementValue instanceof Component) {
                elementClassification = AttributeClassification.EMBEDDED;
                attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
            } else if (elementType.isAssociationType()) {
                elementClassification = isManyToMany ? AttributeClassification.MANY_TO_MANY : AttributeClassification.ONE_TO_MANY;
                attributeClassification = elementClassification;
            } else {
                elementClassification = AttributeClassification.BASIC;
                attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
            }
            final AttributeClassification indexClassification;
            // Finally, we determine the type of the map key (if needed)
            if (value instanceof Map) {
                final Value keyValue = ((Map) value).getIndex();
                final org.hibernate.type.Type keyType = keyValue.getType();
                if (keyType.isAnyType()) {
                    indexClassification = AttributeClassification.ANY;
                } else if (keyValue instanceof Component) {
                    indexClassification = AttributeClassification.EMBEDDED;
                } else if (keyType.isAssociationType()) {
                    indexClassification = AttributeClassification.MANY_TO_ONE;
                } else {
                    indexClassification = AttributeClassification.BASIC;
                }
            } else if (value instanceof List) {
                indexClassification = AttributeClassification.BASIC;
            } else {
                indexClassification = null;
            }
            return new PluralAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, attributeClassification, elementClassification, indexClassification, context);
        } else if (value instanceof OneToMany) {
            // element value within a o.h.mapping.Collection (see logic branch above)
            throw new IllegalArgumentException("HUH???");
        // final boolean isManyToMany = isManyToMany( member );
        // //one to many with FK => entity
        // return new PluralAttributeMetadataImpl(
        // attributeContext.getPropertyMapping(),
        // attributeContext.getOwnerType(),
        // member,
        // isManyToMany
        // ? Attribute.PersistentAttributeType.MANY_TO_MANY
        // : Attribute.PersistentAttributeType.ONE_TO_MANY
        // value,
        // AttributeContext.TypeStatus.ENTITY,
        // Attribute.PersistentAttributeType.ONE_TO_MANY,
        // null, null, null
        // );
        }
    } else if (propertyMapping.isComposite()) {
        // component
        return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.EMBEDDED, context);
    } else {
        // basic type
        return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.BASIC, context);
    }
    throw new UnsupportedMappingException("oops, we are missing something: " + propertyMapping);
}
Also used : AttributeClassification(org.hibernate.metamodel.AttributeClassification) OneToMany(org.hibernate.mapping.OneToMany) UnsupportedMappingException(org.hibernate.metamodel.UnsupportedMappingException) ManagedDomainType(org.hibernate.metamodel.model.domain.ManagedDomainType) AnyType(org.hibernate.type.AnyType) IdentifiableDomainType(org.hibernate.metamodel.model.domain.IdentifiableDomainType) SimpleDomainType(org.hibernate.metamodel.model.domain.SimpleDomainType) JavaType(org.hibernate.type.descriptor.java.JavaType) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) Type(jakarta.persistence.metamodel.Type) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) AbstractIdentifiableType(org.hibernate.metamodel.model.domain.AbstractIdentifiableType) EmbeddableDomainType(org.hibernate.metamodel.model.domain.EmbeddableDomainType) ParameterizedType(java.lang.reflect.ParameterizedType) DomainType(org.hibernate.metamodel.model.domain.DomainType) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) List(org.hibernate.mapping.List) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) Member(java.lang.reflect.Member) MapMember(org.hibernate.metamodel.model.domain.internal.MapMember) Map(org.hibernate.mapping.Map)

Example 2 with UnsupportedMappingException

use of org.hibernate.metamodel.UnsupportedMappingException in project hibernate-orm by hibernate.

the class CompoundNaturalIdMapping method normalizeInput.

@Override
@SuppressWarnings("rawtypes")
public Object[] normalizeInput(Object incoming, SharedSessionContractImplementor session) {
    if (incoming instanceof Object[]) {
        return (Object[]) incoming;
    }
    if (incoming instanceof Map) {
        final Map valueMap = (Map) incoming;
        final List<SingularAttributeMapping> attributes = getNaturalIdAttributes();
        final Object[] values = new Object[attributes.size()];
        for (int i = 0; i < attributes.size(); i++) {
            values[i] = valueMap.get(attributes.get(i).getAttributeName());
        }
        return values;
    }
    throw new UnsupportedMappingException("Do not know how to normalize compound natural-id value : " + incoming);
}
Also used : SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) Map(java.util.Map) UnsupportedMappingException(org.hibernate.metamodel.UnsupportedMappingException)

Example 3 with UnsupportedMappingException

use of org.hibernate.metamodel.UnsupportedMappingException in project hibernate-orm by hibernate.

the class GeneratedValuesProcessor method processGeneratedValues.

public void processGeneratedValues(Object entity, Object id, Object[] state, SharedSessionContractImplementor session) {
    if (selectStatement == null) {
        return;
    }
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final JdbcParameterBindings jdbcParamBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
    int offset = jdbcParamBindings.registerParametersForEachJdbcValue(id, Clause.WHERE, entityDescriptor.getIdentifierMapping(), jdbcParameters, session);
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, selectStatement).translate(jdbcParamBindings, QueryOptions.NONE);
    final List<Object[]> results = session.getFactory().getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParamBindings, new ExecutionContext() {

        @Override
        public SharedSessionContractImplementor getSession() {
            return session;
        }

        @Override
        public QueryOptions getQueryOptions() {
            return QueryOptions.NONE;
        }

        @Override
        public String getQueryIdentifier(String sql) {
            return sql;
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return QueryParameterBindings.NO_PARAM_BINDINGS;
        }

        @Override
        public Callback getCallback() {
            throw new UnsupportedMappingException("Follow-on locking not supported yet");
        }
    }, (row) -> row, ListResultsConsumer.UniqueSemantic.FILTER);
    assert results.size() == 1;
    final Object[] dbSelectionResults = results.get(0);
    for (int i = 0; i < valueDescriptors.size(); i++) {
        final GeneratedValueDescriptor descriptor = valueDescriptors.get(i);
        final Object generatedValue = descriptor.resolver.resolveGeneratedValue(dbSelectionResults, entity, session);
        state[descriptor.attribute.getStateArrayPosition()] = generatedValue;
        descriptor.attribute.getAttributeMetadataAccess().resolveAttributeMetadata(entityDescriptor).getPropertyAccess().getSetter().set(entity, generatedValue);
    }
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) UnsupportedMappingException(org.hibernate.metamodel.UnsupportedMappingException) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

UnsupportedMappingException (org.hibernate.metamodel.UnsupportedMappingException)3 Type (jakarta.persistence.metamodel.Type)1 Member (java.lang.reflect.Member)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Map (java.util.Map)1 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 Collection (org.hibernate.mapping.Collection)1 Component (org.hibernate.mapping.Component)1 List (org.hibernate.mapping.List)1 Map (org.hibernate.mapping.Map)1 OneToMany (org.hibernate.mapping.OneToMany)1 Property (org.hibernate.mapping.Property)1 Value (org.hibernate.mapping.Value)1 AttributeClassification (org.hibernate.metamodel.AttributeClassification)1 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)1 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)1 AbstractIdentifiableType (org.hibernate.metamodel.model.domain.AbstractIdentifiableType)1 DomainType (org.hibernate.metamodel.model.domain.DomainType)1