Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
Created December 6, 2015 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsonsmatt/0384e28017fcdfff9bb3 to your computer and use it in GitHub Desktop.
Save parsonsmatt/0384e28017fcdfff9bb3 to your computer and use it in GitHub Desktop.
/**
* gets a record out of database by ID
*
* infers the type to use based on return
*
* RentalImpl r = Util.getById(10);
*
* @param c The implementation class name
* @param id the id of the instance you want
*/
public static <T> T getById(Long id) {
return (T) DbConfig.withSession(
s -> s.get(((Class) ((ParameterizedType) new Object() { }.getClass().getEnclosingClass().getGenericSuperclass()).getActualTypeArguments()[0]),
id)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment