added subGraph support
This commit is contained in:
@ -30,6 +30,7 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Order;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -43,13 +44,19 @@ public class StandardController extends CommonAbstractController {
|
||||
return em.find(clazz, id);
|
||||
}
|
||||
|
||||
private <T> EntityGraph getEntityGraph(Class<T> clazz, String... subGraphItems) {
|
||||
EntityGraph graph = this.em.createEntityGraph(clazz);
|
||||
if (subGraphItems != null) {
|
||||
for (String subGraphItem : subGraphItems) {
|
||||
graph.addSubgraph(subGraphItem);
|
||||
}
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> T find(Class<T> clazz, Object id, String... subGraphItems) {
|
||||
EntityGraph graph = this.em.createEntityGraph(clazz);
|
||||
for (String subGraphItem : subGraphItems) {
|
||||
graph.addSubgraph(subGraphItem);
|
||||
}
|
||||
|
||||
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||
Map hints = new HashMap<>();
|
||||
hints.put("javax.persistence.loadgraph", graph);
|
||||
|
||||
@ -58,12 +65,12 @@ public class StandardController extends CommonAbstractController {
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields) {
|
||||
return find(clazz, filters, orderFields, 0, 0);
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields, String... subGraphItems) {
|
||||
return find(clazz, filters, orderFields, 0, 0, subGraphItems);
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields, int limit, int offset) {
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields, int limit, int offset, String... subGraphItems) {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
||||
|
||||
@ -80,16 +87,19 @@ public class StandardController extends CommonAbstractController {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
|
||||
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||
query.setHint("javax.persistence.loadgraph", graph);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields) throws ControllerException {
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields, String... subGraphItems) throws ControllerException {
|
||||
return find(clazz, filters, orderFields, 0, 0);
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields, int limit, int offset) throws ControllerException {
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields, int limit, int offset, String... subGraphItems) throws ControllerException {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
||||
|
||||
@ -99,12 +109,19 @@ public class StandardController extends CommonAbstractController {
|
||||
criteria.where(filterCondition);
|
||||
}
|
||||
List<Order> orderList = new ArrayList<>();
|
||||
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
|
||||
if (orderFields != null) {
|
||||
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
|
||||
}
|
||||
final TypedQuery<T> query = em.createQuery(criteria.orderBy(orderList)).setFirstResult(offset);
|
||||
if (limit > 0) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
|
||||
if (!ArrayUtils.isEmpty(subGraphItems)) {
|
||||
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||
query.setHint("javax.persistence.loadgraph", graph);
|
||||
}
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user