Hql 错误:Class 未映射,无法解析符号
Hql errors: Class is not mapped, can't resolve symbol
我正在学习 Hibernate,并且在使用 hql 时遇到困难。我希望我的函数检查数据库中是否存在用户名。
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中class。这是我在 IntelliJ 中的项目布局:
在我的 UserControl class 中,我已经导入了我的用户 class 就像 import entity.User
,但是我仍然不能使用裸 User
class 名称HQL 而不是 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 和页面上的其他人,这表明我的实体 class 的命名存在一些错误,尽管我的实验基于答案没有工作。如果我像上面那样屈服并使用 entity.User
,则会收到此错误:
java.lang.IllegalArgumentException: Could not locate named parameter [userName], expecting one of []
这是我的实体 class:
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>
我错了什么?
我发现发生这种情况的原因是因为我没有在 hibernate.cfg.xml 中映射实体。在 <session-factory> </session-factory>
里面我需要写:
<mapping class="entity.User"/>
我正在学习 Hibernate,并且在使用 hql 时遇到困难。我希望我的函数检查数据库中是否存在用户名。
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中class。这是我在 IntelliJ 中的项目布局:
在我的 UserControl class 中,我已经导入了我的用户 class 就像 import entity.User
,但是我仍然不能使用裸 User
class 名称HQL 而不是 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 和页面上的其他人,这表明我的实体 class 的命名存在一些错误,尽管我的实验基于答案没有工作。如果我像上面那样屈服并使用 entity.User
,则会收到此错误:
java.lang.IllegalArgumentException: Could not locate named parameter [userName], expecting one of []
这是我的实体 class:
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>
我错了什么?
我发现发生这种情况的原因是因为我没有在 hibernate.cfg.xml 中映射实体。在 <session-factory> </session-factory>
里面我需要写:
<mapping class="entity.User"/>