I wrote the following JPA method that queries a database to return a distinct list of a String field (called mtfcc) of a Location object. I then took the list of strings to query for the associated name for that MTFCC.
Could I just modify the first query to get the results, without having to use the for loop to get the associated name field?
@Override
public Map<String, String> getAvailableBorderTypes() {
    // create empty map to store results in. If we don't find results, an empty hashmap will be returned
    Map<String, String> results = new HashMap<String, String>();
    EntityManager em = entityManagerFactory.createEntityManager();
    // get the distinct mtfcc's
    String jpaQuery = "SELECT DISTINCT location.mtfcc FROM Location location";
    // get the distinct mtfcc codes
    List<String> mtfccList = (List<String>) em.createQuery(jpaQuery).getResultList();
    // cycle through the mtfcc list and get the name to associate with it
    for (String mtfcc: mtfccList) {
        String nameQuery = "SELECT DISTINCT location.name FROM Location location WHERE location.mtfcc = ?1";
        String name = (String) em.createQuery(nameQuery).setParameter(1, mtfcc).getSingleResult();
        results.put(mtfcc, name);
    }
    return results;
}
SELECT location FROM Location locationand do the distinct programmatically? Not saying this is the best solution, but right now you are querying all locations twice. \$\endgroup\$SELECT DISTINCT location.mtfcc, location.name FROM Location location. :) Perhaps consider phrasing this as a question on distinct on Stack Overflow if you don't get any knowledgeable responses here. \$\endgroup\$