How can I wrap Play/JPA's Model class with a generic Repository?

久未见 提交于 2019-12-07 11:31:43

问题


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).


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!