So I have this piece of code here:
final CriteriaBuilder builder = JPA.em().getCriteriaBuilder();
final CriteriaQuery<Ticket> criteria = builder.createQuery(Ticket.class);
Root<Ticket> ticketRoot = criteria.from(Ticket.class);
Join<Ticket, Order> order = ticketRoot.join("order", JoinType.LEFT);
Join<Ticket, EventGroup> eventGroups = ticketRoot.join("eventGroup", JoinType.LEFT);
final List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
if (from != null) {
predicates.add(
builder.or(
builder.greaterThanOrEqualTo(order.get("completedOn"), from),
builder.greaterThanOrEqualTo(ticketRoot.get("refundedAt"), from)
)
);
}
It now looks for tickets with completedOn
OR refundedAt
with a date after from
. I want to change this.
What I want to have now is the following:
If a ticket's refundedAt
is not null
, get refundedAt
with date after from
, else get completedOn
with date after from
.
I tried to use builder.selectCase()
, also in combination with criteria.multiSelect()
, but that didn't work.
I hope somebody can help me.
Edit: To make the current situation clear, this is the if/else-statement explained:
IF refundedAt
is not null, get the ticket ONLY if refundedAt
is after from
.
ELSE (refundedAt
is null) get the ticket ONLY if completedOn
is after from
.
Using and
and or
:
predicates.add(
builder.or(
builder.and(
builder.isNotNull(ticketRoot.get("refundedAt")),
builder.greaterThanOrEqualTo(ticketRoot.get("refundedAt"), from)
),
builder.and(
builder.isNull(ticketRoot.get("refundedAt")),
builder.greaterThanOrEqualTo(order.get("completedOn"), from)
)
)
);