Hi,
I could not integrate JpaSpecifications into jmix project. So, I decided to write a service that would execute queries with dynamic conditions:
public class JmixPageRequestService {
@Autowired
private Metadata metadata;
@Autowired
private DataManager dataManager;
public <T> Page<T> load(Class<T> clazz, LogicalCondition condition, Pageable pageable, FetchPlan fetchPlan) {
LoadContext<T> countingContext = prepareLoadContext(clazz);
LoadContext.Query countingQuery = Objects.requireNonNull(countingContext.getQuery());
countingQuery.setCondition(condition);
long count = dataManager.getCount(countingContext);
LoadContext<T> dataContext = prepareLoadContext(clazz);
dataContext.setFetchPlan(fetchPlan);
LoadContext.Query dataQuery = Objects.requireNonNull(dataContext.getQuery());
dataQuery.setCondition(condition);
dataQuery.setFirstResult(pageable.getPageNumber());
dataQuery.setMaxResults(pageable.getPageSize());
List<T> content = dataManager.loadList(dataContext);
return new PageImpl<>(content, pageable, count);
}
private <T> LoadContext<T> prepareLoadContext(Class<T> clazz) {
LoadContext<T> loadContext = new LoadContext<>(metadata.getClass(clazz));
String queryString = "select e from " + clazz.getSimpleName() + " e";
loadContext.setQueryString(queryString);
return loadContext;
}
}
The condition here is a LogicalCondition anded with other conditions. These usually are PropertyConditions. My question is, is it possible to also add a JpqlCondition? If so, how? I have been looking for an example, but failed to find any