gpt4 book ai didi

java - Hql 错误 : Class is not mapped, 无法解析符号

转载 作者:行者123 更新时间:2023-12-02 02:54:31 26 4
gpt4 key购买 nike

我正在学习 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 中的项目布局:

enter image description here

在我的 UserControl 类中,我已经导入了 User 类,例如 importentity.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>

我做错了什么?

最佳答案

我发现发生这种情况的原因是因为我没有在 hibernate.cfg.xml 中映射该实体。里面<session-factory> </session-factory>我需要写:

<mapping class="entity.User"/>

关于java - Hql 错误 : Class is not mapped, 无法解析符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57085692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com