Search in sources :

Example 1 with ManagedType

use of javax.persistence.metamodel.ManagedType in project querydsl by querydsl.

the class JPAPathBuilderValidator method validate.

@Override
public Class<?> validate(Class<?> parent, String property, Class<?> propertyType) {
    try {
        ManagedType managedType = metamodel.managedType(parent);
        Attribute attribute = managedType.getAttribute(property);
        if (attribute instanceof PluralAttribute) {
            return ((PluralAttribute) attribute).getElementType().getJavaType();
        } else {
            return Primitives.wrap(attribute.getJavaType());
        }
    } catch (IllegalArgumentException e) {
        return null;
    }
}
Also used : ManagedType(javax.persistence.metamodel.ManagedType) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute)

Example 2 with ManagedType

use of javax.persistence.metamodel.ManagedType in project hibernate-orm by hibernate.

the class AttributeNodeImpl method internalMakeSubgraph.

@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeSubgraph(Class<X> type) {
    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
        throw new IllegalArgumentException(String.format("Attribute [%s] is not of managed type", getAttributeName()));
    }
    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION) {
        throw new IllegalArgumentException(String.format("Collection elements [%s] is not of managed type", getAttributeName()));
    }
    if (subgraphMap == null) {
        subgraphMap = new HashMap<>();
    }
    final Helper.AttributeSource attributeSource = Helper.resolveAttributeSource(sessionFactory(), managedType);
    final AssociationType attributeType = (AssociationType) attributeSource.findType(attribute.getName());
    final Joinable joinable = attributeType.getAssociatedJoinable(sessionFactory());
    if (joinable.isCollection()) {
        final EntityPersister elementEntityPersister = ((QueryableCollection) joinable).getElementPersister();
        if (type == null) {
            type = elementEntityPersister.getMappedClass();
        } else {
            if (!isTreatableAs(elementEntityPersister, type)) {
                throw new IllegalArgumentException(String.format("Collection elements [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), elementEntityPersister.getMappedClass().getName()));
            }
        }
    } else {
        final EntityPersister entityPersister = (EntityPersister) joinable;
        if (type == null) {
            type = entityPersister.getMappedClass();
        } else {
            if (!isTreatableAs(entityPersister, type)) {
                throw new IllegalArgumentException(String.format("Attribute [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), entityPersister.getMappedClass().getName()));
            }
        }
    }
    ManagedType managedType = null;
    try {
        managedType = sessionFactory.getMetamodel().entity(type.getName());
    } catch (IllegalArgumentException e) {
    // do nothing
    }
    if (managedType == null) {
        managedType = attribute.getDeclaringType();
    }
    final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, managedType, type);
    subgraphMap.put(type, subgraph);
    return subgraph;
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) Helper(org.hibernate.metamodel.internal.Helper) EntityPersister(org.hibernate.persister.entity.EntityPersister) ManagedType(javax.persistence.metamodel.ManagedType) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Aggregations

ManagedType (javax.persistence.metamodel.ManagedType)2 Attribute (javax.persistence.metamodel.Attribute)1 PluralAttribute (javax.persistence.metamodel.PluralAttribute)1 CollectionHelper (org.hibernate.internal.util.collections.CollectionHelper)1 Helper (org.hibernate.metamodel.internal.Helper)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 Joinable (org.hibernate.persister.entity.Joinable)1 AssociationType (org.hibernate.type.AssociationType)1