我正在尝试使用jpa检查用户的凭据。每次运行代码时,我都会得到:
java.lang.illegalargumentexception:namedName的查询:找不到user.checkcredentials。
我做了很多尝试,但我找不到如何找出导致错误的原因。
(我在mysql上创建了这个数据库,我称之为“游戏化营销”)
索引.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;
...
持久性.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>
暂无答案!
目前还没有任何答案,快来回答吧!