我需要有关Java中简单servlet的帮助。 我在HTML文件中有一个登录表单(电子邮件和密码字段),一个Java servlet和一个用于登录女巫的实用程序,如果数据库中存在一对使用相同电子邮件和密码的夫妇,则将对其进行检查。 运行该程序,查询中出现语法错误,错误提示“用户未映射”。 我认为这是一个虚拟错误,有人可以帮我吗?
这里是模型。用户类
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table( name = "USERS",
uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_id", nullable=false, unique=true)
private int id;
@Column(name="firstName", length=40, nullable=false)
private String firstName;
@Column(name="lastname", length=40, nullable=false)
private String lastName;
@Column(name="email", length=40, nullable=false)
private String email;
@Column(name="password", length=40, nullable=false)
private String password;
public User(String firstName, String lastName, String email, String password){
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getFirstName(){
return this.firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return this.lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getEmail(){
return this.email;
}
public void setEmail(String email){
this.email = email;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.password = password;
}
}
这里是实用程序。LoginDao类
public static boolean validateCredentials(String email, String password){
Session session = DbService.getSession();
session.beginTransaction();
Query query = session.createQuery
("from Users u where u.email=:email and u.password=:password");
query.setParameter("email", email);
query.setParameter("password", password);
List list=query.list();
...
这是我的LoginServlet doPost()方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
String password = request.getParameter("password");
if(LoginDao.validateCredentials(email, password)){
RequestDispatcher view = request.getRequestDispatcher("SigninSuccess.jsp");
view.forward(request, response);
}
else{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
}
}
在这里signin.html表单文件
<form method="POST" action="LoginServlet">
<p>
<label for="fname">E-mail</label>
<input type="text" name="email" placeholder="e-mail">
</p>
<p>
<label for="lname">Password</label>
<input type="password" name="password" placeholder="password">
</p>
<input type="submit" value="Login">
<p class="message">Not registered? <a href="./signup.html">Create an account</a></p>
</form>
这是错误:
未映射用户[从用户u,其中u.email =:email和p.password =:password] 在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133) 在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) ...
答案 0 :(得分:2)
这里您正在使用数据库中的表名(用户)创建Query
-
Query query = session.createQuery
("from Users u where u.email=:email and u.password=:password");
这就是为什么您得到此错误。您需要使用实体名称-User
-
Query query = session.createQuery
("from User u where u.email=:email and u.password=:password");
HQL期望您正在使用Java类名和Java属性名。您还必须注意使用的是javax.persistence.Entity
而不是org.hibernate.annotations.Entity
答案 1 :(得分:0)
Razib为您提供了正确的答案,告诉您在查询中使用User
而不是Users
。如果仍然遇到相同的错误,则可能仅是因为Hibernate尚未找到@Entity
类。
请运行以下代码并响应输出。
sessionFactory.getConfiguration()
.getClassMappings()
.forEachRemaining(pc -> System.out.println(pc.getEntityName() + "\t" + pc.getTable().getName()));
提示:要获取sessionFactory
,只需将其自动连接到服务或dAO中即可:
@Autowired
private LocalSessionFactoryBean sessionFactory;
答案 2 :(得分:0)
终于我解决了我的问题。我忘记在LoginDao类的配置中添加config.addAnnotatedClass(model.User.class);
。谢谢大家。