@Override public JpaMetamodelAccessor getJpaMetamodelAccessor() { return jpaProvider.getJpaMetamodelAccessor(); } }
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()); }
private List<List<Attribute<?, ?>>> getAndCheckAttributes(String[] attributes) { List<List<Attribute<?, ?>>> attrs = new ArrayList<List<Attribute<?, ?>>>(attributes.length); for (int i = 0; i < attributes.length; i++) { if (attributes[i] == null) { throw new NullPointerException("attribute at position " + i); } if (attributes[i].isEmpty()) { throw new IllegalArgumentException("empty attribute at position " + i); } JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); attrs.add(jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attributes[i]).getAttributes()); } return attrs; }
private String findCorrelatedAttribute(JoinNode correlationParent, List<PathElementExpression> pathElements, int start, int end, List<PathElementExpression> fields) { PathTargetResolvingExpressionVisitor pathResolvingVisitor = new PathTargetResolvingExpressionVisitor(metamodel, correlationParent.getNodeType(), correlationParent.getAlias()); for (int i = start; i < end; i++) { PathElementExpression expression = pathElements.get(i); expression.accept(pathResolvingVisitor); Attribute<?, ?> attribute = pathResolvingVisitor.getPossibleTargets().entrySet().iterator().next().getKey(); if (attribute != null) { if (mainQuery.jpaProvider.getJpaMetamodelAccessor().isJoinable(attribute)) { StringBuilder sb = new StringBuilder(); for (PathElementExpression field : fields) { sb.append(field.toString()); sb.append('.'); } sb.append(attribute.getName()); return sb.toString(); } else { fields.add(expression); } } } return null; }
@Override public SimpleReturningBuilder returning(String modificationQueryAttribute) { if (modificationQueryAttribute == null) { throw new NullPointerException("modificationQueryAttribute"); } if (modificationQueryAttribute.isEmpty()) { throw new IllegalArgumentException("Invalid empty modificationQueryAttribute"); } 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 Attribute<?, ?> idAttribute = JpaMetamodelUtils.getSingleIdAttribute(entityType); modificationQueryAttribute = idAttribute.getName(); } JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); List<Attribute<?, ?>> attributePath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, modificationQueryAttribute).getAttributes(); if (returningAttributes.put(modificationQueryAttribute, attributePath) != null) { throw new IllegalArgumentException("The entity attribute [" + modificationQueryAttribute + "] has already been returned!"); } return this; }
public static AttributeAccessor forEntityMapping(EntityViewManagerImpl evm, Class<?> entityClass, String mapping) { if (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); }
@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()); TypedQuery<Object[]> exampleQuery = getExampleQuery(attributes); String[] returningColumns = getReturningColumns(attributes); return getExecuteWithReturningQuery(exampleQuery, baseQuery, returningColumns, null); }
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); if (jpaMetamodelAccessor.isJoinable(lastPathElem)) { sb.append(entityAlias).append('.');
modificationQueryAttribute = idAttribute.getName(); } else { JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); AttributePath queryAttributePath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, modificationQueryAttribute); queryAttrType = queryAttributePath.getAttributeClass();
AttributePath basicAttributePath = context.getJpaProvider().getJpaMetamodelAccessor().getAttributePath(context.getEntityMetamodel(), managedType, mapping); List<Attribute<?, ?>> attributes = basicAttributePath.getAttributes(); for (int i = 1; i < attributes.size(); i++) {
private void visitKeyOrIndexExpression(PathExpression pathExpression) { JoinNode node = (JoinNode) pathExpression.getBaseNode(); Attribute<?, ?> attribute = node.getParentTreeNode().getAttribute(); // Exclude element collections as they are not problematic if (!jpaProvider.getJpaMetamodelAccessor().isElementCollection(attribute)) { // There are weird mappings possible, we have to check if the attribute is a join table if (jpaProvider.getJoinTable(node.getParent().getEntityType(), attribute.getName()) != null) { keyRestrictedLeftJoins.add(node); } } } }
@Override public Expression visit(MapKeyExpression expression) { if (expression == expressionToSplit) { List<PathElementExpression> expressions = new ArrayList<>(2); expressions.add(expression); for (String subAttributePart : subAttribute.split("\\.")) { expressions.add(new PropertyExpression(subAttributePart)); } JoinNode node = ((JoinNode) expression.getPath().getBaseNode()).getKeyJoinNode(); String field = subAttribute; Class<?> fieldClass = jpaProvider.getJpaMetamodelAccessor().getAttributePath(metamodel, node.getManagedType(), field).getAttributeClass(); Type<?> fieldType = metamodel.type(fieldClass); return new PathExpression( expressions, new SimplePathReference(node, field, fieldType), false, false ); } return expression; }
JpaProvider jpaProvider = queryBuilder.mainQuery.jpaProvider; EntityMetamodel metamodel = queryBuilder.mainQuery.metamodel; JpaMetamodelAccessor jpaMetamodelAccessor = jpaProvider.getJpaMetamodelAccessor();
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)); } } }
private JoinResult createOrUpdateNode(JoinNode baseNode, List<String> joinRelationAttributes, String treatType, String alias, JoinType joinType, boolean implicit, boolean defaultJoin, boolean joinAllowed) { Type<?> baseNodeType = baseNode.getNodeType(); String joinRelationName = StringUtils.join(".", joinRelationAttributes); JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); AttributeHolder attrJoinResult = JpaUtils.getAttributeForJoining(metamodel, baseNodeType, expressionFactory.createJoinPathExpression(joinRelationName), baseNode.getAlias()); Attribute<?, ?> attr = attrJoinResult.getAttribute();
final boolean isElementCollection = jpaProvider.getJpaMetamodelAccessor().isElementCollection(targetAttribute);
Map<String, ? extends ExtendedAttribute<?, ?>> ownedAttributes; String prefix = field; if (baseNode.getParentTreeNode() != null && jpaProvider.getJpaMetamodelAccessor().isElementCollection(baseNode.getParentTreeNode().getAttribute())) { String elementCollectionPath = baseNode.getParentTreeNode().getRelationName(); ExtendedManagedType entityManagedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getParent().getEntityType());
@Override public Expression visit(PathExpression expression) { if (expression.getBaseNode() == null) { Expression aliasedExpression = ((SelectInfo) aliasManager.getAliasInfo(expression.toString())).getExpression(); Expression newExpression = aliasedExpression.accept(this); return aliasedExpression == newExpression ? expression : newExpression; } if (expression == expressionToSplit) { List<PathElementExpression> expressions = new ArrayList<>(expression.getExpressions()); for (String subAttributePart : subAttribute.split("\\.")) { expressions.add(new PropertyExpression(subAttributePart)); } String field; if (expression.getField() == null) { field = subAttribute; } else { field = expression.getField() + "." + subAttribute; } JoinNode node = (JoinNode) expression.getBaseNode(); Class<?> fieldClass = jpaProvider.getJpaMetamodelAccessor().getAttributePath(metamodel, node.getManagedType(), field).getAttributeClass(); Type<?> fieldType = metamodel.type(fieldClass); return new PathExpression( expressions, new SimplePathReference(node, field, fieldType), expression.isUsedInCollectionFunction(), expression.isCollectionKeyPath() ); } return expression; }
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); AttributeHolder attributeHolder = JpaUtils.getAttributeForJoining(metamodel, baseNodeType, expressionFactory.createJoinPathExpression(attributeName), baseNode.getAlias()); Attribute<?, ?> attr = attributeHolder.getAttribute();