Skip to content

Instantly share code, notes, and snippets.

@ripper234
Created November 16, 2011 18:12
Show Gist options
  • Select an option

  • Save ripper234/1370851 to your computer and use it in GitHub Desktop.

Select an option

Save ripper234/1370851 to your computer and use it in GitHub Desktop.
Working JPA repository skeleton in Play
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