Created
April 26, 2012 08:24
-
-
Save jhiemer/2497598 to your computer and use it in GitHub Desktop.
Fetch Impl for relation
This file contains 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 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