Search in sources :

Example 1 with PluralAttributeImpl

use of org.hibernate.metamodel.internal.PluralAttributeImpl in project hibernate-orm by hibernate.

the class AttributeNodeImpl method internalMakeKeySubgraph.

@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeKeySubgraph(Class<X> type) {
    if (!attribute.isCollection()) {
        throw new IllegalArgumentException(String.format("Non-collection attribute [%s] cannot be target of key subgraph", getAttributeName()));
    }
    final PluralAttributeImpl pluralAttribute = (PluralAttributeImpl) attribute;
    if (pluralAttribute.getCollectionType() != PluralAttribute.CollectionType.MAP) {
        throw new IllegalArgumentException(String.format("Non-Map attribute [%s] cannot be target of key subgraph", getAttributeName()));
    }
    final AssociationType attributeType = (AssociationType) Helper.resolveType(sessionFactory(), attribute);
    final QueryableCollection collectionPersister = (QueryableCollection) attributeType.getAssociatedJoinable(sessionFactory());
    final Type indexType = collectionPersister.getIndexType();
    if (!indexType.isAssociationType()) {
        throw new IllegalArgumentException(String.format("Map index [%s] is not an entity; cannot be target of key subgraph", getAttributeName()));
    }
    if (keySubgraphMap == null) {
        keySubgraphMap = new HashMap<>();
    }
    final AssociationType indexAssociationType = (AssociationType) indexType;
    final EntityPersister indexEntityPersister = (EntityPersister) indexAssociationType.getAssociatedJoinable(sessionFactory());
    if (type == null) {
        type = indexEntityPersister.getMappedClass();
    } else {
        if (!isTreatableAs(indexEntityPersister, type)) {
            throw new IllegalArgumentException(String.format("Map key [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), indexEntityPersister.getMappedClass().getName()));
        }
    }
    final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, attribute.getDeclaringType(), type);
    keySubgraphMap.put(type, subgraph);
    return subgraph;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ManagedType(javax.persistence.metamodel.ManagedType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) PluralAttributeImpl(org.hibernate.metamodel.internal.PluralAttributeImpl) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Aggregations

ManagedType (javax.persistence.metamodel.ManagedType)1 PluralAttributeImpl (org.hibernate.metamodel.internal.PluralAttributeImpl)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 AssociationType (org.hibernate.type.AssociationType)1 Type (org.hibernate.type.Type)1