Search in sources :

Example 6 with JpaMetamodelAccessor

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

the class BaseUpdateCriteriaBuilderImpl method addAttribute.

protected void addAttribute(String attributeName) {
    // Just do that to assert the attribute exists
    JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
    jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attributeName);
    Integer attributeBindIndex = setAttributeBindingMap.get(attributeName);
    if (attributeBindIndex != null) {
        throw new IllegalArgumentException("The attribute [" + attributeName + "] has already been bound!");
    }
    setAttributeBindingMap.put(attributeName, selectManager.getSelectInfos().size());
}
Also used : JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor)

Example 7 with JpaMetamodelAccessor

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

the class AbstractFullQueryBuilder method addAttributes.

private void addAttributes(String prefix, Set<SingularAttribute<?, ?>> attributes, List<ResolvedExpression> resolvedExpressions, StringBuilder sb, JoinNode rootNode) {
    for (SingularAttribute<?, ?> attribute : attributes) {
        String attributeName;
        if (prefix.isEmpty()) {
            attributeName = attribute.getName();
        } else {
            attributeName = prefix + attribute.getName();
        }
        if (attribute.getType() instanceof EmbeddableType<?>) {
            Set<SingularAttribute<?, ?>> subAttributes = new TreeSet<>(ATTRIBUTE_NAME_COMPARATOR);
            subAttributes.addAll(((EmbeddableType<?>) attribute.getType()).getSingularAttributes());
            addAttributes(attributeName + ".", subAttributes, resolvedExpressions, sb, rootNode);
        } else {
            sb.setLength(0);
            rootNode.appendDeReference(sb, attributeName, false);
            PathExpression expression = (PathExpression) rootNode.createExpression(attributeName);
            JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
            expression.setPathReference(new SimplePathReference(rootNode, attributeName, getMetamodel().type(jpaMetamodelAccessor.getAttributePath(getMetamodel(), rootNode.getManagedType(), attributeName).getAttributeClass())));
            resolvedExpressions.add(new ResolvedExpression(sb.toString(), expression));
        }
    }
}
Also used : EmbeddableType(javax.persistence.metamodel.EmbeddableType) SingularAttribute(javax.persistence.metamodel.SingularAttribute) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) TreeSet(java.util.TreeSet) JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor)

Example 8 with JpaMetamodelAccessor

use of com.blazebit.persistence.spi.JpaMetamodelAccessor 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 9 with JpaMetamodelAccessor

use of com.blazebit.persistence.spi.JpaMetamodelAccessor 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 10 with JpaMetamodelAccessor

use of com.blazebit.persistence.spi.JpaMetamodelAccessor 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)

Aggregations

JpaMetamodelAccessor (com.blazebit.persistence.spi.JpaMetamodelAccessor)11 SingularAttribute (javax.persistence.metamodel.SingularAttribute)6 ExtendedAttribute (com.blazebit.persistence.spi.ExtendedAttribute)5 Attribute (javax.persistence.metamodel.Attribute)4 CustomReturningSQLTypedQuery (com.blazebit.persistence.impl.query.CustomReturningSQLTypedQuery)3 CustomSQLQuery (com.blazebit.persistence.impl.query.CustomSQLQuery)3 AttributePath (com.blazebit.persistence.spi.AttributePath)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)2 EntityType (javax.persistence.metamodel.EntityType)2 PluralAttribute (javax.persistence.metamodel.PluralAttribute)2 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)1 Expression (com.blazebit.persistence.parser.expression.Expression)1 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)1 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)1 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)1 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)1