Search in sources :

Example 1 with AttributePath

use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.

the class MethodAttributeMapping method determineMappedBy.

public String determineMappedBy(ManagedType<?> managedType, String mapping, MetamodelBuildingContext context, EmbeddableOwner embeddableMapping) {
    String mappedBy;
    if (embeddableMapping == null) {
        mappedBy = this.mappedBy;
    } else {
        if (embeddableMappedByMap == null) {
            embeddableMappedByMap = new HashMap<>(1);
        }
        mappedBy = embeddableMappedByMap.get(embeddableMapping);
    }
    if (mappedBy != null) {
        if (mappedBy.isEmpty()) {
            return null;
        }
        return mappedBy;
    }
    if (embeddableMapping == null) {
        this.mappedBy = "";
    } else {
        embeddableMappedByMap.put(embeddableMapping, "");
    }
    if (mapping == null || mapping.isEmpty()) {
        return null;
    }
    if (!(managedType instanceof EntityType<?>)) {
        // Can't determine the inverse mapped by attribute of a non-entity
        return null;
    }
    try {
        AttributePath basicAttributePath = context.getJpaProvider().getJpaMetamodelAccessor().getAttributePath(context.getEntityMetamodel(), managedType, mapping);
        List<Attribute<?, ?>> attributes = basicAttributePath.getAttributes();
        for (int i = 1; i < attributes.size(); i++) {
            if (attributes.get(i).getDeclaringType().getPersistenceType() != Type.PersistenceType.EMBEDDABLE) {
                // If the mapping goes over a non-embeddable, we can't determine a mapped by attribute name
                return null;
            }
        }
        mappedBy = context.getJpaProvider().getMappedBy((EntityType<?>) managedType, mapping);
        if (embeddableMapping == null) {
            this.mappedBy = mappedBy;
        } else {
            embeddableMappedByMap.put(embeddableMapping, mappedBy);
        }
        return mappedBy;
    } catch (IllegalArgumentException ex) {
        // if the mapping is invalid, we skip the determination as the error will be analyzed further at a later stage
        return null;
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) MappingMethodMultiListAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodMultiListAttribute) CorrelatedMethodSingularAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodSingularAttribute) MappingMethodListAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodListAttribute) MappingMethodMultiMapAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodMultiMapAttribute) Attribute(javax.persistence.metamodel.Attribute) SubqueryMethodSingularAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.SubqueryMethodSingularAttribute) PluralAttribute(com.blazebit.persistence.view.metamodel.PluralAttribute) CorrelatedMethodMultiListAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodMultiListAttribute) CorrelatedMethodCollectionAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodCollectionAttribute) CorrelatedMethodListAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodListAttribute) MappingMethodSingularAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodSingularAttribute) MappingMethodSetAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodSetAttribute) CorrelatedMethodMapAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodMapAttribute) MappingMethodCollectionAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodCollectionAttribute) MappingMethodMapAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.MappingMethodMapAttribute) CorrelatedMethodMultiMapAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodMultiMapAttribute) CorrelatedMethodSetAttribute(com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodSetAttribute) AttributePath(com.blazebit.persistence.spi.AttributePath)

Example 2 with AttributePath

use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.

the class AbstractModificationCriteriaBuilder method getWithReturningQuery.

@SuppressWarnings("unchecked")
public <Z> TypedQuery<ReturningResult<Z>> getWithReturningQuery(String attribute, Class<Z> type) {
    if (attribute == null) {
        throw new NullPointerException("attribute");
    }
    if (type == null) {
        throw new NullPointerException("type");
    }
    if (attribute.isEmpty()) {
        throw new IllegalArgumentException("Invalid empty attribute");
    }
    JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
    AttributePath attrPath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attribute);
    if (!type.isAssignableFrom(attrPath.getAttributeClass())) {
        throw new IllegalArgumentException("The given expected field type is not of the expected type: " + attrPath.getAttributeClass().getName());
    }
    List<List<Attribute<?, ?>>> attributes = new ArrayList<List<Attribute<?, ?>>>();
    attributes.add(attrPath.getAttributes());
    Query baseQuery = em.createQuery(getBaseQueryStringWithCheck(null, null));
    TypedQuery<Object[]> exampleQuery = getExampleQuery(attributes);
    String[] returningColumns = getReturningColumns(attributes);
    return getExecuteWithReturningQuery(exampleQuery, baseQuery, returningColumns, null);
}
Also used : TypedQuery(javax.persistence.TypedQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) Query(javax.persistence.Query) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor) AttributePath(com.blazebit.persistence.spi.AttributePath)

Example 3 with AttributePath

use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.

the class AbstractModificationCriteriaBuilder method returning.

@SuppressWarnings("unchecked")
public X returning(String cteAttribute, String modificationQueryAttribute) {
    if (cteAttribute == null) {
        throw new NullPointerException("cteAttribute");
    }
    if (modificationQueryAttribute == null) {
        throw new NullPointerException("modificationQueryAttribute");
    }
    if (cteAttribute.isEmpty()) {
        throw new IllegalArgumentException("Invalid empty cteAttribute");
    }
    if (modificationQueryAttribute.isEmpty()) {
        throw new IllegalArgumentException("Invalid empty modificationQueryAttribute");
    }
    ExtendedAttribute attributeEntry = attributeEntries.get(cteAttribute);
    if (attributeEntry == null) {
        if (cteType.getAttribute(cteAttribute) != null) {
            throw new IllegalArgumentException("Can't bind the embeddable cte attribute [" + cteAttribute + "] directly! Please bind the respective sub attributes.");
        }
        throw new IllegalArgumentException("The cte attribute [" + cteAttribute + "] does not exist!");
    }
    Class<?> queryAttrType;
    if (isReturningEntityAliasAllowed && modificationQueryAttribute.equals(entityAlias)) {
        // Our little special case, since there would be no other way to refer to the id as the object type
        queryAttrType = entityType.getJavaType();
        Attribute<?, ?> idAttribute = JpaMetamodelUtils.getSingleIdAttribute(entityType);
        modificationQueryAttribute = idAttribute.getName();
    } else {
        JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
        AttributePath queryAttributePath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, modificationQueryAttribute);
        queryAttrType = queryAttributePath.getAttributeClass();
    }
    Class<?> cteAttrType = attributeEntry.getElementClass();
    // but if it already supports the returning clause, it can only also support returning all columns
    if (!cteAttrType.isAssignableFrom(queryAttrType)) {
        throw new IllegalArgumentException("The given cte attribute '" + cteAttribute + "' with the type '" + cteAttrType.getName() + "'" + " can not be assigned with a value of the type '" + queryAttrType.getName() + "' of the query attribute '" + modificationQueryAttribute + "'!");
    }
    if (returningAttributeBindingMap.put(cteAttribute, modificationQueryAttribute) != null) {
        throw new IllegalArgumentException("The cte attribute [" + cteAttribute + "] has already been bound!");
    }
    for (String column : attributeEntry.getColumnNames()) {
        if (columnBindingMap.put(column, cteAttribute) != null) {
            throw new IllegalArgumentException("The cte column [" + column + "] has already been bound!");
        }
    }
    return (X) this;
}
Also used : ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor) AttributePath(com.blazebit.persistence.spi.AttributePath)

Example 4 with AttributePath

use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.

the class BaseUpdateCriteriaBuilderImpl method getForeignKeyParticipatingQueries.

protected Collection<Query> getForeignKeyParticipatingQueries() {
    Map<String, Query> map = null;
    JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
    for (String attributeName : setAttributeBindingMap.keySet()) {
        AttributePath path = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attributeName);
        for (Attribute<?, ?> attributePart : path.getAttributes()) {
            if (attributePart instanceof SingularAttribute<?, ?>) {
                SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attributePart;
                if (map == null) {
                    map = new HashMap<>();
                }
                if (singularAttribute.getType() instanceof EntityType<?>) {
                    String entityName = ((EntityType<?>) singularAttribute.getType()).getName();
                    if (!map.containsKey(entityName)) {
                        map.put(entityName, em.createQuery("select e from " + entityName + " e"));
                    }
                    break;
                }
            }
        }
    }
    return map == null ? Collections.<Query>emptyList() : map.values();
}
Also used : EntityType(javax.persistence.metamodel.EntityType) SingularAttribute(javax.persistence.metamodel.SingularAttribute) TypedQuery(javax.persistence.TypedQuery) CustomSQLQuery(com.blazebit.persistence.impl.query.CustomSQLQuery) Query(javax.persistence.Query) CustomReturningSQLTypedQuery(com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery) JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor) AttributePath(com.blazebit.persistence.spi.AttributePath)

Example 5 with AttributePath

use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.

the class Accessors method forEntityMapping.

public static AttributeAccessor forEntityMapping(EntityViewManagerImpl evm, Class<?> entityClass, String mapping) {
    if (mapping == null || mapping.isEmpty()) {
        return new NestedAttributeAccessor(Collections.<AttributeAccessor>emptyList());
    }
    EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel();
    AttributePath path = evm.getJpaProvider().getJpaMetamodelAccessor().getBasicAttributePath(entityMetamodel, entityMetamodel.managedType(entityClass), mapping);
    List<Attribute<?, ?>> attributes = path.getAttributes();
    if (attributes.size() == 1) {
        return forEntityAttribute(evm, entityClass, attributes.get(0), null);
    }
    List<AttributeAccessor> mappers = new ArrayList<>(attributes.size());
    Class<?> targetClass = entityClass;
    for (int i = 0; i < attributes.size() - 1; i++) {
        javax.persistence.metamodel.Attribute<?, ?> attribute = attributes.get(i);
        Class<?> attributeClass = JpaMetamodelUtils.resolveFieldClass(targetClass, attribute);
        mappers.add(forEntityAttribute(evm, targetClass, attribute, attributeClass));
        targetClass = attributeClass;
    }
    mappers.add(forEntityAttribute(evm, targetClass, attributes.get(attributes.size() - 1), null));
    return new NestedAttributeAccessor(mappers);
}
Also used : Attribute(javax.persistence.metamodel.Attribute) AbstractMethodAttribute(com.blazebit.persistence.view.impl.metamodel.AbstractMethodAttribute) MethodAttribute(com.blazebit.persistence.view.metamodel.MethodAttribute) SingularAttribute(com.blazebit.persistence.view.metamodel.SingularAttribute) MappingAttribute(com.blazebit.persistence.view.metamodel.MappingAttribute) ArrayList(java.util.ArrayList) EntityMetamodel(com.blazebit.persistence.parser.EntityMetamodel) AttributePath(com.blazebit.persistence.spi.AttributePath)

Aggregations

AttributePath (com.blazebit.persistence.spi.AttributePath)6 Attribute (javax.persistence.metamodel.Attribute)4 JpaMetamodelAccessor (com.blazebit.persistence.spi.JpaMetamodelAccessor)3 ArrayList (java.util.ArrayList)3 EntityType (javax.persistence.metamodel.EntityType)3 SingularAttribute (javax.persistence.metamodel.SingularAttribute)3 CustomReturningSQLTypedQuery (com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)2 CustomSQLQuery (com.blazebit.persistence.impl.query.CustomSQLQuery)2 ExtendedAttribute (com.blazebit.persistence.spi.ExtendedAttribute)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2 EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)1 ListIndexAttribute (com.blazebit.persistence.parser.ListIndexAttribute)1 MapKeyAttribute (com.blazebit.persistence.parser.MapKeyAttribute)1 AbstractMethodAttribute (com.blazebit.persistence.view.impl.metamodel.AbstractMethodAttribute)1 CorrelatedMethodCollectionAttribute (com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodCollectionAttribute)1 CorrelatedMethodListAttribute (com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodListAttribute)1 CorrelatedMethodMapAttribute (com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodMapAttribute)1 CorrelatedMethodMultiListAttribute (com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodMultiListAttribute)1 CorrelatedMethodMultiMapAttribute (com.blazebit.persistence.view.impl.metamodel.attribute.CorrelatedMethodMultiMapAttribute)1