@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); }
throw new IllegalArgumentException("Only access to the owner type's id attribute '" + idAttribute.getName() + "' is allowed. Invalid access to different attribute through the expression: " + attributePath); return new AttributePath(new ArrayList<Attribute<?, ?>>(Collections.singletonList(idAttribute)), com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), idAttribute)); Class<?> targetClass = com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), collectionAttribute); if (dotIndex == -1) { return new AttributePath(new ArrayList<Attribute<?, ?>>(Collections.singletonList(collectionAttribute)), targetClass); throw new IllegalArgumentException("Only access to the target element type's id attribute '" + actualIdAttributeName + "' is allowed. Invalid access to different attribute through the expression: " + attributePath); return new AttributePath(new ArrayList<>(Arrays.asList(collectionAttribute, idAttribute)), com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(targetClass, idAttribute)); } else { Attribute<?, ?> attribute = null; throw new IllegalArgumentException("Couldn't find attribute '" + collectionElementAttributeName + "' on managed type '" + targetClass.getName() + "'. Invalid access through the expression: " + attributePath, cause); return new AttributePath(new ArrayList<>(Arrays.asList(collectionAttribute, attribute)), com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(targetClass, attribute)); return new AttributePath(new ArrayList<Attribute<?, ?>>(Collections.singletonList(collectionFunction)), collectionFunction.getJavaType());
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; }
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); AttributePath queryAttributePath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, modificationQueryAttribute); queryAttrType = queryAttributePath.getAttributeClass();
@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; }
@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; }
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); }
throw new IllegalArgumentException("Only access to the owner type's id attribute '" + idAttribute.getName() + "' is allowed. Invalid access to different attribute through the expression: " + attributePath); return new AttributePath(new ArrayList<Attribute<?, ?>>(Collections.singletonList(idAttribute)), com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), idAttribute)); Class<?> targetClass = com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), collectionAttribute); if (dotIndex == -1) { return new AttributePath(new ArrayList<Attribute<?, ?>>(Collections.singletonList(collectionAttribute)), targetClass); throw new IllegalArgumentException("Only access to the target element type's id attribute '" + actualIdAttributeName + "' is allowed. Invalid access to different attribute through the expression: " + attributePath); return new AttributePath(new ArrayList<>(Arrays.asList(collectionAttribute, idAttribute)), com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(targetClass, idAttribute)); } else { Attribute<?, ?> attribute = null; throw new IllegalArgumentException("Couldn't find attribute '" + collectionElementAttributeName + "' on managed type '" + targetClass.getName() + "'. Invalid access through the expression: " + attributePath, cause); return new AttributePath(new ArrayList<>(Arrays.asList(collectionAttribute, attribute)), com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(targetClass, attribute)); return new AttributePath(new ArrayList<Attribute<?, ?>>(Collections.singletonList(collectionFunction)), collectionFunction.getJavaType());
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)); } } }
List<Attribute<?, ?>> attributes = basicAttributePath.getAttributes(); for (int i = 1; i < attributes.size(); i++) { if (attributes.get(i).getDeclaringType().getPersistenceType() != Type.PersistenceType.EMBEDDABLE) {
return new AttributePath(attrPath, com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), attribute)); } else { attrPath = new ArrayList<Attribute<?, ?>>(); return new AttributePath(attrPath, currentClass);
@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; }
return new AttributePath(attrPath, com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), attribute)); } else { attrPath = new ArrayList<Attribute<?, ?>>(); return new AttributePath(attrPath, currentClass);
return new AttributePath(attrPath, com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), attribute)); } else { attrPath = new ArrayList<>(); return new AttributePath(attrPath, currentClass);
return new AttributePath(attrPath, com.blazebit.persistence.parser.util.JpaMetamodelUtils.resolveFieldClass(type.getJavaType(), attribute)); } else { attrPath = new ArrayList<>(); return new AttributePath(attrPath, currentClass);