问题描述
我正在尝试使用 JPA 对用户进行凭据检查。 每次运行代码,我得到:
java.lang.IllegalArgumentException: NamedQuery of name: 未找到 User.checkCredentials。
我已经做了很多尝试,但我找不到如何找出导致错误的原因。
(我在 MysqL 上创建了数据库,我称之为“gamified_marketing”)
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<Meta charset="UTF-8">
<link rel="stylesheet" href="mystyle.css">
<title>Login page</title>
</head>
<body>
<h1>Welcome back to the Gamified Marketing application</h1>
<form action="CheckLogin" method="POST">
Username: <input type="text" name="usr" required> <br>
Password: <input type="password" name="pwd" required> <br>
<input type="submit" value="login">
<p th:text=" ${errorMsg}"></p>
</form>
</body>
</html>
检查登录
@WebServlet("/CheckLogin")
public class CheckLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB(name = "it.polimi.db2.services/UserService")
private UserService usrService;
public CheckLogin() {
super();
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws servletexception,IOException {
String usr = null;
String pwd = null;
try {
usr = request.getParameter("usr");
pwd = request.getParameter("pwd");
if (usr == null || pwd == null || usr.isEmpty() || pwd.isEmpty()) {
throw new Exception("Missing or empty credential value");
}
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Missing credential value");
return;
}
User user;
try {
user = usrService.checkCredentials(usr,pwd);
} catch (CredentialsException | NonUniqueResultException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Could not check credentials");
return;
}
用户服务
@Stateless
public class UserService {
@PersistenceContext(unitName = "GamifiedMarketingApplicationEJB")
private EntityManager em;
public UserService() {}
public User checkCredentials(String usr,String pwd) throws CredentialsException,NonUniqueResultException {
List<User> uList = null;
try {
uList = em.createNamedQuery("User.checkCredentials",User.class).setParameter(1,usr).setParameter(2,pwd).getResultList();
} catch (PersistenceException e) {
throw new CredentialsException("Could not verify credentals");
}
if (uList.isEmpty())
return null;
else if (uList.size() == 1)
return uList.get(0);
throw new NonUniqueResultException("More than one user registered with same credentials");
}
}
@Entity
@Table(name = "Userr",schema = "gamified_marketing")
@NamedQuery(name = "User.checkCredentials",query = "SELECT r FROM User r WHERE r.username = ?1 and r.password = ?2")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String username;
private String email;
private String password;
private String lastLogin;
...
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="GamifiedMarketingApplicationEJB"
transaction-type="JTA">
<jta-data-source>GamifiedMarketing</jta-data-source>
<class>it.polimi.db2.entities.User</class>
<properties>
<property name="eclipselink.logging.level" value="FINE" />
</properties>
</persistence-unit>
</persistence>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)