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; }
@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); }
} else { JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor(); AttributePath queryAttributePath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, modificationQueryAttribute); queryAttrType = queryAttributePath.getAttributeClass();