Search in sources :

Example 1 with CriteriaConversionException

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method getSpecialCaseQueryBuilder.

/**
 * Use an alternate approach to generating a fetch query for a collection located inside of an @Embeddable object. Related
 * to https://hibernate.atlassian.net/browse/HHH-8802. The alternate approach leverages HQL rather than JPA criteria,
 * which seems to alleviate the problem.
 *
 * @param embeddedCollectionPath the path to the collection field itself
 * @param filterMappings all the fetch restrictions for this request
 * @param collectionClass the type of the collection members
 * @return the builder capable of generating an appropriate HQL query
 */
protected TypedQueryBuilder getSpecialCaseQueryBuilder(FieldPath embeddedCollectionPath, List<FilterMapping> filterMappings, String collectionClass) {
    String specialPath = embeddedCollectionPath.getTargetProperty();
    String[] pieces = specialPath.split("\\.");
    if (pieces.length != 3) {
        throw new CriteriaConversionException(String.format("Expected to find a target property of format [embedded field].[collection field].[property] for the embedded collection path (%s)", specialPath), embeddedCollectionPath);
    }
    String expression = specialPath.substring(0, specialPath.lastIndexOf("."));
    TypedQueryBuilder builder;
    try {
        builder = new TypedQueryBuilder(Class.forName(collectionClass), "specialEntity").addJoin(new TQJoin("specialEntity." + expression, "embeddedCollection"));
    } catch (Exception e) {
        throw ExceptionHelper.refineException(e);
    }
    for (TQRestriction restriction : buildSpecialRestrictions(expression, filterMappings)) {
        builder = builder.addRestriction(restriction);
    }
    for (TQRestriction restriction : buildStandardRestrictions(embeddedCollectionPath, filterMappings)) {
        builder = builder.addRestriction(restriction);
    }
    for (FilterMapping mapping : filterMappings) {
        if (mapping.getSortDirection() != null) {
            String mappingProperty = mapping.getFieldPath() == null ? null : mapping.getFieldPath().getTargetProperty();
            if (StringUtils.isEmpty(mappingProperty)) {
                mappingProperty = mapping.getFullPropertyName();
            }
            builder = builder.addOrder(new TQOrder("specialEntity." + mappingProperty, SortDirection.ASCENDING == mapping.getSortDirection()));
        }
    }
    return builder;
}
Also used : CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) TQOrder(org.broadleafcommerce.common.util.dao.TQOrder) TypedQueryBuilder(org.broadleafcommerce.common.util.dao.TypedQueryBuilder) TQJoin(org.broadleafcommerce.common.util.dao.TQJoin) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) TQRestriction(org.broadleafcommerce.common.util.dao.TQRestriction)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 TQJoin (org.broadleafcommerce.common.util.dao.TQJoin)1 TQOrder (org.broadleafcommerce.common.util.dao.TQOrder)1 TQRestriction (org.broadleafcommerce.common.util.dao.TQRestriction)1 TypedQueryBuilder (org.broadleafcommerce.common.util.dao.TypedQueryBuilder)1 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)1 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)1 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)1 CriteriaConversionException (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException)1 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)1 BeansException (org.springframework.beans.BeansException)1