在安全登录后尝试访问任何页面时发生JPAException

时间:2011-06-06 15:39:26

标签: jpa playframework

在我使用Play!的网站上,我有一个管理员部分。所有管理员控制器都有@With@Check注释。

断开连接时,一切都很顺利。 连接后,每次加载页面(任何页面,管理员与否)时,都会引发JPAException


这是管理员检查器:

public class Security extends Secure.Security {

    static boolean authentify(String username, String password) {
        return User.connect(username, password) != null;
    }

    static boolean check(String profile) {
        User user = User.find("byLogin", connected()).<User>first(); /**line 12**/
        renderArgs.put("user", user.login);
        if ("admin".equals(profile)) {
            return user.role == Role.Admin;
        } else if ("manager".equals(profile)) {
            return user.role == Role.Manager || user.role == Role.Admin;
        } else if ("user".equals(profile)) {
            return true;
        }
        return false;
    }
}

以下是例外:

JPA error
A JPA error occurred (The JPA context is not initialized. JPA Entity Manager automatically start when one or more classes annotated with the @javax.persistence.Entity annotation are found in the application.): 

play.exceptions.JPAException: The JPA context is not initialized. JPA Entity Manager automatically start when one or more classes annotated with the @javax.persistence.Entity annotation are found in the application.
at play.db.jpa.JPA.get(JPA.java:22)
at play.db.jpa.JPA.em(JPA.java:51)
at play.db.jpa.JPQL.em(JPQL.java:16)
at play.db.jpa.JPQL.find(JPQL.java:44)
at models.User.find(User.java)
at controllers.Security.check(Security.java:12)
at play.utils.Java.invokeStaticOrParent(Java.java:159)
at controllers.Secure$Security.invoke(Secure.java:193)
at {play}/framework/templates/tags/404.html.(line:1)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:213)
at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:347)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:213)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:247)
at play.templates.Template.render(Template.java:26)
at play.server.PlayHandler.serve404(PlayHandler.java:634)
at Invocation.HTTP Request(Play!)

这实际上并没有打扰应用程序(它工作正常),但我真的不知道为什么会发生这种情况......

提前致谢!


编辑:

用户类代码:

@Entity
public class User extends Model {

    @Email
    public String email;

    @Required @Password
    public String password;

    public String fullname;

    @Required @Column(unique=true)
    public String login;

    @Enumerated(EnumType.STRING)
    public Role role=Role.User;

    public static User connect(String login, String password) {
        return find("byLoginAndPassword", login, password).first();
    }

    @Override
    public String toString() {
        return login;
    }
}

1 个答案:

答案 0 :(得分:0)

您是否在 application.conf

中取消注释了以下代码?
db=mem

仅当在application.conf中激活数据库时,Play才会启动JPA上下文。