Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Repository
- public class RepositoryWithQueryBuilder {
- @PersistenceContext
- EntityManager entityManager;
- List<Customer> findCustomers(boolean northAmericans, int minAge, SocialMedia socialMedia) {
- if (minAge <= 0) {
- throw new IllegalArgumentException("minAge must be a positive number");
- }
- String queryString = "SELECT c FROM Customer c, CustomerSocialMedia sm WHERE ";
- if (northAmericans) {
- queryString += "c.countryCode IN ('US', 'CA') ";
- } else {
- queryString = "c.countryCode NOT IN ('US', 'CA') ";
- }
- if (socialMedia != SocialMedia.TWITTER && socialMedia != SocialMedia.FACEBOOK) {
- throw new IllegalArgumentException("The system only supports Twitter and Facebook at the moment");
- } else if (minAge < 65) {
- queryString += "c.socialMedia = " + socialMedia;
- } // else any social media for seniors
- val query = entityManager.createQuery(queryString, Customer.class);
- return query.getResultList();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement