-
-
Save codylerum/6851dd5882b0d306fcea323a7cfe31e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestScoped | |
public class SummaryAccountQueries implements Serializable { | |
public Optional<SummaryAccountProjection> bad(final SummaryBillingAccount summaryAccount, final UnifiedBillingParty unifiedBillingParty, final LocalDate date) { | |
try { | |
CriteriaBuilder builder = em.getCriteriaBuilder(); | |
CriteriaQuery<SummaryAccountProjection> query = builder.createQuery(SummaryAccountProjection.class); | |
Root<SummaryAccountProjection> root = query.from(SummaryAccountProjection.class); | |
List<Predicate> predicates = new ArrayList<>(); | |
predicates.add(builder.equal(root.get(SummaryAccountProjection_.summaryAccount), summaryAccount)); | |
predicates.add(builder.equal(root.get(SummaryAccountProjection_.unifiedBillingParty), unifiedBillingParty)); | |
predicates.add(builder.equal(root.get(SummaryAccountProjection_.date), date)); | |
query.where(predicates.toArray(new Predicate[predicates.size()])); | |
return Optional.of(em.createQuery(query).getSingleResult()); | |
} | |
catch (NoResultException e) { | |
return Optional.empty(); | |
} | |
} | |
public Optional<SummaryAccountProjection> good(final SummaryBillingAccount summaryAccount, final UnifiedBillingParty unifiedBillingParty, final LocalDate date) { | |
try { | |
CriteriaBuilder builder = em.getCriteriaBuilder(); | |
CriteriaQuery<SummaryAccountProjection> query = builder.createQuery(SummaryAccountProjection.class); | |
Root<SummaryAccountProjection> root = query.from(SummaryAccountProjection.class); | |
List<Predicate> predicates = new ArrayList<>(); | |
predicates.add(builder.equal(root.get(SummaryAccountProjection_.summaryAccount), summaryAccount)); | |
if (unifiedBillingParty != null) { | |
predicates.add(builder.equal(root.get(SummaryAccountProjection_.unifiedBillingParty), unifiedBillingParty)); | |
} | |
else { | |
predicates.add(builder.isNull(root.get(SummaryAccountProjection_.unifiedBillingParty))); | |
} | |
predicates.add(builder.equal(root.get(SummaryAccountProjection_.date), date)); | |
query.where(predicates.toArray(new Predicate[predicates.size()])); | |
return Optional.of(em.createQuery(query).getSingleResult()); | |
} | |
catch (NoResultException e) { | |
return Optional.empty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this
and
Behave differently. Shouldn't an NPE be thrown if passing a null into an equality predicate?