Search in sources :

Example 11 with JpaMetamodelAccessor

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

the class JoinManager method createOrUpdateNode.

private JoinResult createOrUpdateNode(JoinNode baseNode, List<String> joinRelationAttributes, String treatType, String alias, JoinType joinType, JoinNode currentJoinNode, boolean implicit, boolean defaultJoin, boolean joinAllowed, boolean singularJoinAllowed) {
    Type<?> baseNodeType = baseNode.getNodeType();
    String joinRelationName = StringUtils.join(".", joinRelationAttributes);
    JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
    AttributeHolder attrJoinResult = JpaUtils.getAttributeForJoining(metamodel, baseNodeType, expressionFactory.createJoinPathExpression(joinRelationName), null);
    Attribute<?, ?> attr = attrJoinResult.getAttribute();
    if (attr == null) {
        throw new IllegalArgumentException("Field with name " + joinRelationName + " was not found within class " + JpaMetamodelUtils.getTypeName(baseNodeType));
    }
    if (!jpaMetamodelAccessor.isJoinable(attr)) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine(new StringBuilder("Field with name ").append(joinRelationName).append(" of class ").append(JpaMetamodelUtils.getTypeName(baseNodeType)).append(" is parseable and therefore it has not to be fetched explicitly.").toString());
        }
        return new JoinResult(baseNode, joinRelationAttributes, attrJoinResult.getAttributeType(), -1, -1);
    }
    // Don't throw implicit join not allowed exception when the currentJoinNode contains the node that we would create in this method
    if (!joinAllowed && !baseNode.hasDefaultJoin(joinRelationName) && (currentJoinNode == null || currentJoinNode == baseNode || !currentJoinNode.containsNode(baseNode, joinRelationName) && (!singularJoinAllowed || attr instanceof PluralAttribute<?, ?, ?>))) {
        throw new ImplicitJoinNotAllowedException(baseNode, joinRelationAttributes, treatType);
    }
    if (implicit || (defaultJoin && alias == null)) {
        String aliasToUse = alias == null ? attr.getName() : alias;
        alias = baseNode.getAliasInfo().getAliasOwner().generateJoinAlias(aliasToUse);
    }
    if (joinType == null) {
        joinType = getModelAwareType(baseNode, attr);
    }
    Type<?> joinRelationType = attrJoinResult.getAttributeType();
    JoinNode newNode = getOrCreate(baseNode, joinRelationName, joinRelationType, treatType, alias, joinType, "Ambiguous implicit join", implicit, defaultJoin, attr);
    return new JoinResult(newNode);
}
Also used : PluralAttribute(javax.persistence.metamodel.PluralAttribute) JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor)

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