Skip to content

Instantly share code, notes, and snippets.

@geraldhumphries
Last active December 3, 2015 08:36
Show Gist options
  • Save geraldhumphries/9fba746a4613aae78ca4 to your computer and use it in GitHub Desktop.
Save geraldhumphries/9fba746a4613aae78ca4 to your computer and use it in GitHub Desktop.
fetch spec
@And({@Spec(path = "field1", spec = In.class),
@Spec(path = "field2", spec = In.class),
@Spec(path = "fetch", spec = FetchSpecification.class, constVal = {"lazyRelationship1", "lazyRelationship2"})})
Specification<Entity> entitySpec,
/**
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.inteloom.jed.web.rest.util;
import javax.persistence.criteria.*;
import net.kaczmarzyk.spring.data.jpa.domain.PathSpecification;
import java.util.Arrays;
import java.util.List;
/**
* <p>Extension to specification-arg-resolver to allow fetching in a specification
*
* @author Tomasz Kaczmarzyk
* @author Gerald Humphries
*/
public class FetchSpecification<T> extends PathSpecification<T> {
private List<String> expectedValues;
public FetchSpecification(String path, String[] httpParamValues) {
this(path, httpParamValues, null);
}
public FetchSpecification(String path, String[] httpParamValues, String[] config) {
super(path);
if (httpParamValues == null || httpParamValues.length < 1) {
throw new IllegalArgumentException();
}
expectedValues = Arrays.asList(httpParamValues);
}
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
if (Long.class != query.getResultType()) { // do not join in count queries
for (String value : expectedValues){
root.fetch(value, JoinType.LEFT);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment