Created
November 16, 2011 18:12
-
-
Save ripper234/1370851 to your computer and use it in GitHub Desktop.
Working JPA repository skeleton in Play
This file contains hidden or 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
| package repositories; | |
| import play.db.jpa.GenericModel; | |
| import play.db.jpa.Model; | |
| import play.utils.Java; | |
| import java.lang.reflect.ParameterizedType; | |
| public abstract class BaseRepository<T extends Model> { | |
| private Class<T> modelClass; | |
| public BaseRepository(){ | |
| ParameterizedType superclass = (ParameterizedType) getClass().getGenericSuperclass(); | |
| modelClass = (Class<T>)superclass.getActualTypeArguments()[0]; | |
| } | |
| public void save(T object) { | |
| object.save(); | |
| } | |
| public T findOne(String query, Object... params) { | |
| try { | |
| GenericModel.JPAQuery result = runJPAMethod("find", query, params); | |
| return result.first(); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| private GenericModel.JPAQuery runJPAMethod(String method, String query, Object[] params) throws Exception { | |
| return (GenericModel.JPAQuery) Java.invokeStatic(modelClass, method, query, params); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment