I am learning Hibernate, and having a difficult time with hql. I want my function to check if a userName exists in the database.
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;
}
The above function lies in my UserControl class. This is my project layout in IntelliJ:
In my UserControl class, I have imported my User class like import entity.User
, but still I cannot use the bare User
class name in the HQL instead of entity.User
without getting the following error.
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select 1 from User u where u.userName = :userName]
My searching for similar questions has pointed me to this answer and the others on the page which suggest that there is some mistake in the naming of my Entity class though my experiments based on the answer have not worked. If I give in and use entity.User
as I have above, then I get this error:
java.lang.IllegalArgumentException: Could not locate named parameter [userName], expecting one of []
This is my Entity 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; }
}
And my 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>
My 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>
What have I got wrong?