【发布时间】:2015-07-02 10:54:23
【问题描述】:
我正在使用@With(Action.class) 注释来拦截对特定控制器/动作的调用。我正在尝试在拦截器函数中从数据库中检索会话;但是 JPA 助手类在 Action.class 拦截器方法“调用”中不可用。
有人可以指导如何在拦截器函数中检索数据库实体吗?
谢谢。
拦截器类:
public class SecuredAction extends Simple {
public SecuredAction() {
// TODO Auto-generated constructor stub
}
@Override
public Promise<Result> call(Context ctx) throws Throwable {
// check isContactVerified/isEmailVerified
String sid = getSidFromCookie(ctx);
if (sid != null) {
Session appSession = (Session) JPA.em().createNamedQuery("Session.findBySessionId").getSingleResult();
User user = appSession.getUserId();
if (user != null) {
ctx.args.put("user", user);
return delegate.call(ctx);
}
}
Result unauthorized = Results.unauthorized("Invalid Session");
return F.Promise.pure(unauthorized);
}
private String getSidFromCookie(Http.Context ctx) {
return ctx.session().get(AppConstants.COOKIE_USER_SESSIONID);
}
}
错误: [RuntimeException:没有 EntityManager 绑定到此线程。尝试使用 @play.db.jpa.Transactional 注释您的操作方法]
【问题讨论】:
标签: java jpa playframework playframework-2.0