Created
November 16, 2011 16:45
-
-
Save ripper234/1370600 to your computer and use it in GitHub Desktop.
findOne wrapper for find() JPA method
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 T findOne(String query, Object... params) { | |
try { | |
ParameterizedType superclass = (ParameterizedType) getClass().getGenericSuperclass(); | |
Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0]; | |
Method findMethod = aClass.getMethod("find", String.class, Object[].class); | |
Object[] arr = new Object[2]; | |
arr[0] = query; | |
arr[1] = params; | |
System.arraycopy(params, 0, arr, 1, params.length); | |
Object resultObj = findMethod.invoke(null, arr); | |
GenericModel.JPAQuery result = (GenericModel.JPAQuery) resultObj; | |
return result.first(); | |
// GenericModel.JPAQuery result = T.find(query, params); | |
// return result.first(); | |
} 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