I am working on a project Spring and Java, generated using JHipster. I encounter a problem with the following items of Spring: Specification, Criteria, and filtering a list of items.
I have a functioning SQL query, than I want to "translate" as a Spring Specification. But, I don't find how to do it, as the query works on 3 tables, and has 2 conditions.
Basically, the work concerns 3 tables: contract, transfer & entry. A contract can be inside one or several transfers, and a transfer contains 1 to several contracts. An entry is the link between these two tables (an entry contains a contract_id and a transfer_id).
The needs is to use the specification to get a list of contracts which are linked to a not received transfer.
How can i write this?
I have already looked at several stackoverflow posts and questions, but I found answers for a join between only two tables, or how to write specifications on an entity.
Here, the query I want to translate:
SELECT c.*
FROM contract AS c
LEFT JOIN entry AS e ON e.contract_id = c.id
INNER JOIN transfer AS t ON t.id = e.transfer_id
AND t.status != 'RECEIVED'
Here, an example of the existing Contract Specification created by JHipster Here you can see how the JHipster specification are used as filter. I want to add the new specification inside the already existing ones
private Specification<Contract> createSpecification(ContractCriteria criteria) {
Specification<Contract> specification = Specification.where(null);
if (criteria == null) {
return specification;
}
return specification.and(buildStringSpecification(criteria.getContractNumber(), Contract_.contractNumber))
.and(buildSpecification(criteria.getStatus(), Contract_.status))
.and(buildSpecification(
criteria.getStoreCode(),
root -> root.join(Contract_.store, JoinType.LEFT).get(Store_.code)));