Skip to content

Instantly share code, notes, and snippets.

@jhiemer
Created April 26, 2012 08:24
Show Gist options
  • Save jhiemer/2497598 to your computer and use it in GitHub Desktop.
Save jhiemer/2497598 to your computer and use it in GitHub Desktop.
Fetch Impl for relation
public void fetch(String association, Object entity) {
if(association == null || entity == null){
return;
}
String hql = Utils.appendAll("SELECT n.", association, " FROM ", entity.getClass().getSimpleName(), " n WHERE n.id = :id").toString();
Query query = em.createQuery(hql);
try {
Object id = PropertyUtils.getProperty(entity, "id");
query.setParameter("id", id);
List<?> result = query.getResultList();
Object associationType = PropertyUtils.getProperty(entity, association);
if (associationType instanceof Set<?>) {
@SuppressWarnings({ "unchecked", "rawtypes" })
Set<?> localResult = new HashSet(result);
PropertyUtils.setProperty(entity, association, localResult);
}
else
PropertyUtils.setProperty(entity, association, result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment