Skip to content

Instantly share code, notes, and snippets.

@codylerum
Last active March 5, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codylerum/6851dd5882b0d306fcea323a7cfe31e0 to your computer and use it in GitHub Desktop.
Save codylerum/6851dd5882b0d306fcea323a7cfe31e0 to your computer and use it in GitHub Desktop.
@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();
}
}
}
@codylerum
Copy link
Author

In this

predicates.add(builder.equal(root.get(SummaryAccountProjection_.unifiedBillingParty), null));

and

predicates.add(builder.isNull(root.get(SummaryAccountProjection_.unifiedBillingParty)));

Behave differently. Shouldn't an NPE be thrown if passing a null into an equality predicate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment