Last active
August 29, 2015 14:15
-
-
Save gabfssilva/136e5623bcb0d1f9e8a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class Repository<T> | |
public Repository<T> fromEntity(){ | |
return this; | |
} | |
public Where<T> where(){ | |
Map<String, Object> parameters = new HashMap<String, Object>; | |
return new Where<T>(parameters); | |
} | |
public static class Where<T> { | |
private Map<String, Object> parameters; | |
private String field; | |
private Object value; | |
public Where(Map<String, Object> parameters){ | |
this.parameters = parameters; | |
} | |
public Where<T> field(String field){ | |
this.field = field; | |
} | |
public Where<T> equalTo(Object value){ | |
this.value = value; | |
return this; | |
} | |
public Where<T> and(){ | |
this.parameters.put(field, value); | |
return new Where<T>(this.parameters); | |
} | |
public Then<T> then(){ | |
return new Then<T>(this.parameters); | |
} | |
} | |
public static class Then<T>{ | |
private Map<String, Object> parameters; | |
public Then(Map<String, Object> parameters){ | |
this.parameters = parameters; | |
} | |
public List<T> get(){ | |
return fetchBy(fields); | |
} | |
private List<T> fetchBy(Map<String, Object> fields) { | |
Root<T> root = criteriaQuery.from(entity); | |
fields.forEach((field, value) -> { | |
criteriaQuery.where(criteriaBuilder.equal(root.get(field), value)); | |
}); | |
return em.createQuery(criteriaQuery).getResultList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment