Advertisement
Guest User

Untitled

a guest
May 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. @Repository
  2. public class RepositoryWithQueryBuilder {
  3.  
  4. @PersistenceContext
  5. EntityManager entityManager;
  6.  
  7. List<Customer> findCustomers(boolean northAmericans, int minAge, SocialMedia socialMedia) {
  8. if (minAge <= 0) {
  9. throw new IllegalArgumentException("minAge must be a positive number");
  10. }
  11.  
  12. String queryString = "SELECT c FROM Customer c, CustomerSocialMedia sm WHERE ";
  13. if (northAmericans) {
  14. queryString += "c.countryCode IN ('US', 'CA') ";
  15. } else {
  16. queryString = "c.countryCode NOT IN ('US', 'CA') ";
  17. }
  18.  
  19. if (socialMedia != SocialMedia.TWITTER && socialMedia != SocialMedia.FACEBOOK) {
  20. throw new IllegalArgumentException("The system only supports Twitter and Facebook at the moment");
  21. } else if (minAge < 65) {
  22. queryString += "c.socialMedia = " + socialMedia;
  23. } // else any social media for seniors
  24.  
  25. val query = entityManager.createQuery(queryString, Customer.class);
  26. return query.getResultList();
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement