我正在学习Hibernate,并在hql中度过了一段困难的时光。我希望我的函数检查数据库中是否存在userName。
private boolean userExists(Session session, String userName) {
String hql = "select 1 from entity.User u where u.userName = :userName";
Query query = session.createQuery(hql);
query.setParameter("userName", userName);
return query.uniqueResult() != null;
}上面的函数位于我的UserControl类中。这是我在IntelliJ中的项目布局:

在我的UserControl类中,我已经像import entity.User一样导入了我的用户类,但是在没有得到以下错误的情况下,我仍然不能在HQL中使用原始的User类名而不是entity.User。
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select 1 from User u where u.userName = :userName]我对类似问题的搜索指向了this answer和页面上的其他人,这表明在命名实体类时存在一些错误,尽管基于答案的实验没有奏效。如果我像上面那样让步并使用entity.User,那么我就会得到以下错误:
java.lang.IllegalArgumentException: Could not locate named parameter [userName], expecting one of []这是我的实体类:
package entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_association", schema = "login_register_view")
public class User {
private int id;
private String userName;
private String password;
private String color;
private Integer pocketCount;
private Double weight;
@Id
@Column(name= "id", nullable = false)
public int getId(){ return id; }
public void setId(int id){ this.id = id;}
@Column(name = "user_name")
public String getUserName(){ return userName; }
public void setUserName(String userName){ this.userName = userName; }
@Column(name = "password")
public String getPassword(){ return password; }
public void setPassword(String password){ this.password = password; }
@Column(name = "color")
public String getColor(){ return color; }
public void setColor(String color){ this.color = color; }
@Column(name = "pocket_count")
public Integer getPocketCount(){ return pocketCount; }
public void setPocketCount(Integer pocketCount){
this.pocketCount = pocketCount;
}
@Column(name = "weight")
public Double getWeight(){ return weight; }
public void setWeight(Double weight){ this.weight = weight; }
}还有我的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/login_register_view?createDatabaseIfNotExist=true</property>
<property name="connection.driver_class">
com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL8Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>UserControl</servlet-name>
<servlet-class>control.UserControl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserControl</servlet-name>
<url-pattern>/user-control</url-pattern>
</servlet-mapping>
</web-app>我出什么问题了?
发布于 2019-07-19 05:26:01
我发现发生这种情况的原因是因为我没有在我的hibernate.cfg.xml中映射实体。在<session-factory> </session-factory>内部,我需要写:
<mapping class="entity.User"/>https://stackoverflow.com/questions/57085692
复制相似问题