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.Order;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.criteria.Root;
|
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);
|
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)
|
@Lock(LockType.READ)
|
||||||
public <T extends Serializable> T find(Class<T> clazz, Object id, String... subGraphItems) {
|
public <T extends Serializable> T find(Class<T> clazz, Object id, String... subGraphItems) {
|
||||||
EntityGraph graph = this.em.createEntityGraph(clazz);
|
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||||
for (String subGraphItem : subGraphItems) {
|
|
||||||
graph.addSubgraph(subGraphItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map hints = new HashMap<>();
|
Map hints = new HashMap<>();
|
||||||
hints.put("javax.persistence.loadgraph", graph);
|
hints.put("javax.persistence.loadgraph", graph);
|
||||||
|
|
||||||
@ -58,12 +65,12 @@ public class StandardController extends CommonAbstractController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields) {
|
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);
|
return find(clazz, filters, orderFields, 0, 0, subGraphItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Lock(LockType.READ)
|
@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 CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
||||||
|
|
||||||
@ -80,16 +87,19 @@ public class StandardController extends CommonAbstractController {
|
|||||||
query.setMaxResults(limit);
|
query.setMaxResults(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||||
|
query.setHint("javax.persistence.loadgraph", graph);
|
||||||
|
|
||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Lock(LockType.READ)
|
@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);
|
return find(clazz, filters, orderFields, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Lock(LockType.READ)
|
@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 CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
||||||
|
|
||||||
@ -99,12 +109,19 @@ public class StandardController extends CommonAbstractController {
|
|||||||
criteria.where(filterCondition);
|
criteria.where(filterCondition);
|
||||||
}
|
}
|
||||||
List<Order> orderList = new ArrayList<>();
|
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);
|
final TypedQuery<T> query = em.createQuery(criteria.orderBy(orderList)).setFirstResult(offset);
|
||||||
if (limit > 0) {
|
if (limit > 0) {
|
||||||
query.setMaxResults(limit);
|
query.setMaxResults(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!ArrayUtils.isEmpty(subGraphItems)) {
|
||||||
|
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||||
|
query.setHint("javax.persistence.loadgraph", graph);
|
||||||
|
}
|
||||||
|
|
||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user