|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package eu.eudat.queryable.jpa.hibernatequeryablelist;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import eu.eudat.queryable.QueryableList;
|
|
|
|
|
import eu.eudat.queryable.collector.Collector;
|
|
|
|
|
import eu.eudat.queryable.exceptions.NotSingleResultException;
|
|
|
|
@ -33,6 +34,7 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
private List<NestedQuerySinglePredicate<T>> nestedPredicates = new LinkedList<>();
|
|
|
|
|
private boolean distinct = false;
|
|
|
|
|
private List<OrderByPredicate<T>> orderings = new LinkedList<>();
|
|
|
|
|
private List<GroupByPredicate<T>> groupings = new LinkedList<>();
|
|
|
|
|
private List<String> fields = new LinkedList<>();
|
|
|
|
|
private Integer length;
|
|
|
|
|
private Integer offset;
|
|
|
|
@ -150,6 +152,11 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QueryableList<T> groupBy(GroupByPredicate<T> predicate) {
|
|
|
|
|
this.groupings.add(predicate);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Long count() {
|
|
|
|
|
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
|
|
|
|
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
|
|
|
|
@ -205,6 +212,14 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
return predicates.toArray(new Order[predicates.size()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Expression[] generateGroupPredicates(List<GroupByPredicate<T>> groupByPredicates, Root<T> root) {
|
|
|
|
|
List<Expression> predicates = new LinkedList<>();
|
|
|
|
|
for (GroupByPredicate<T> groupPredicate : groupByPredicates) {
|
|
|
|
|
predicates.add(groupPredicate.applyPredicate(this.manager.getCriteriaBuilder(), root));
|
|
|
|
|
}
|
|
|
|
|
return predicates.toArray(new Expression[predicates.size()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<T> toList() {
|
|
|
|
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
|
|
|
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
|
|
|
@ -212,22 +227,20 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
this.root = this.query.from(this.tClass);
|
|
|
|
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
|
|
|
|
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
|
|
|
|
if (!this.groupings.isEmpty()) this.query.groupBy(this.generateGroupPredicates(this.groupings, this.root));
|
|
|
|
|
if (!this.fields.isEmpty()) this.selectFields();
|
|
|
|
|
if (distinct) this.query.distinct(true);
|
|
|
|
|
//if (!this.fields.isEmpty()) return this.toListWithFields();
|
|
|
|
|
//if (!this.fields.isEmpty()) this.query.multiselect(this.parseFields(this.fields));
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
if (!this.fields.isEmpty()) return this.toListWithFields().stream().map(m -> mapper.convertValue(m, this.tClass)).collect(Collectors.toList());
|
|
|
|
|
return this.toListWithOutFields();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Map> toListWithFields() {
|
|
|
|
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
|
|
|
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
|
|
|
|
else this.query = builder.createQuery(this.tClass);
|
|
|
|
|
this.root = this.query.from(this.tClass);
|
|
|
|
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
|
|
|
|
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
|
|
|
|
if (!this.fields.isEmpty()) this.selectFields();
|
|
|
|
|
if (distinct) this.query.distinct(true);
|
|
|
|
|
List<Tuple> results = this.manager.createQuery(query).getResultList();
|
|
|
|
|
TypedQuery<Tuple> typedQuery = this.manager.createQuery(this.query);
|
|
|
|
|
if (this.offset != null) typedQuery.setFirstResult(this.offset);
|
|
|
|
|
if (this.length != null) typedQuery.setMaxResults(this.length);
|
|
|
|
|
List<Tuple> results = typedQuery.getResultList();
|
|
|
|
|
Map<Object, List<Tuple>> groupedResults = results.stream()
|
|
|
|
|
.collect(Collectors.groupingBy(x -> x.get("id")));
|
|
|
|
|
return this.collector.buildFromTuple(results, groupedResults, this.fields, "id");
|
|
|
|
@ -252,6 +265,7 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
this.root = this.query.from(this.tClass);
|
|
|
|
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
|
|
|
|
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
|
|
|
|
if (!this.groupings.isEmpty()) this.query.groupBy(this.generateGroupPredicates(this.groupings, this.root));
|
|
|
|
|
if (!this.fields.isEmpty()) this.selectFields();
|
|
|
|
|
if (distinct) this.query.distinct(true);
|
|
|
|
|
if (!this.fields.isEmpty()) return this.toListAsyncWithFields();
|
|
|
|
@ -356,6 +370,8 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
if (!this.orderings.isEmpty())
|
|
|
|
|
criteriaQuery.orderBy(this.generateOrderPredicates(this.orderings, criteriaRoot));
|
|
|
|
|
|
|
|
|
|
if (!this.groupings.isEmpty()) criteriaQuery.groupBy(this.generateGroupPredicates(this.groupings, this.root));
|
|
|
|
|
|
|
|
|
|
TypedQuery<T> typedQuery = this.manager.createQuery(criteriaQuery);
|
|
|
|
|
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
|
|
|
|
return typedQuery;
|
|
|
|
@ -466,4 +482,11 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
|
|
|
|
.createQuery(update)
|
|
|
|
|
.executeUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Path[] parseFields(List<String> selectedFields) {
|
|
|
|
|
List<Path> paths = new ArrayList<>();
|
|
|
|
|
selectedFields.forEach(s -> paths.add(root.get(s)));
|
|
|
|
|
|
|
|
|
|
return paths.toArray(new Path[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|