gpt4 book ai didi

安全登录后尝试访问任何页面时发生 JPA 异常

转载 作者:行者123 更新时间:2023-12-04 23:47:20 26 4
gpt4 key购买 nike

在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @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;
}
}

这里是异常(exception):

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;
}
}

最佳答案

您是否在 application.conf 中取消注释以下代码?

db=mem

只有在 application.conf 中激活了数据库,Play 才会启动 JPA 上下文。

关于安全登录后尝试访问任何页面时发生 JPA 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com