我正在学习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类中。这是我在IntelliJ中的项目布局:
在UserControl类中,我已经导入了import entity.User
之类的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类的命名存在一些错误,尽管我根据答案进行的实验没有用。如果我像上面那样屈服并使用entity.User
,则会收到此错误:
java.lang.IllegalArgumentException: Could not locate named parameter [userName], expecting one of []
这是我的Entity类:
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>
我怎么了?
答案 0 :(得分:0)
我发现发生这种情况的原因是因为我没有在hibernate.cfg.xml中映射该实体。在<session-factory> </session-factory>
里面,我需要写:
<mapping class="entity.User"/>