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;
}
}
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;
}
Aggregations