I don't like working with models objects directly, because this breaks encapsulation. Instead, I prefer the Repository Pattern.
When I try to implement a simple repository
public abstract class BaseRepository<T extends Model> {
public T findOne(String query, Object... params) {
GenericModel.JPAQuery result = T.find(query, params);
return result.first();
}
}
public class UserRepository extends BaseRepository<User>{}
UserRepository repo = new UserRepository();
repo.findOne("byUsername", "test");
I get exceptions because of the way java's generic or JPA annotations work:
java.lang.UnsupportedOperationException: Please annotate your JPA model with
@javax.persistence.Entity annotation.
at play.db.jpa.GenericModel.find(GenericModel.java:269)
at repositories.BaseRepository.findOne(BaseRepository.java:12)
Is there a way around this?
(Needless to say the model is properly annotated, and when I use it directly with say User.find("byUsername", "test").first() it works well).
It does not work because you are calling the static (class) method.
The JPAEnhancer will at run time add the method to your class (User) but there is no polymorphism for types / static methods, it will always call the GenericModel one.
What you can try to do is get the actual parametric type with something like
ParameterizedType superclass = (ParameterizedType) getClass().getGenericSuperclass();
Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
and the invoke method on that class..
Hope that helps...
来源:https://stackoverflow.com/questions/8151864/how-can-i-wrap-play-jpas-model-class-with-a-generic-repository