我正在學(xué)習(xí) Hibernate,并且在使用 hql 時(shí)遇到了困難。我希望我的函數(shù)檢查數(shù)據(jù)庫中是否存在用戶名。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; }上面的函數(shù)位于我的 UserControl 類中。這是我在 IntelliJ 中的項(xiàng)目布局:在我的 UserControl 類中,我已經(jīng)導(dǎo)入了我的 User 類import entity.User,但我仍然不能User在 HQL 中使用裸類名而不是entity.User沒有得到以下錯(cuò)誤。java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select 1 from User u where u.userName = :userName]我對類似問題的搜索讓我找到了這個(gè)答案和頁面上的其他人,這表明我的實(shí)體類的命名存在一些錯(cuò)誤,盡管我基于答案的實(shí)驗(yàn)沒有奏效。entity.User如果我像上面那樣屈服并使用,那么我會收到此錯(cuò)誤:java.lang.IllegalArgumentException: Could not locate named parameter [userName], expecting one of []這是我的實(shí)體類: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; }}
1 回答

胡子哥哥
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
我發(fā)現(xiàn)發(fā)生這種情況的原因是因?yàn)槲覜]有在我的 hibernate.cfg.xml 中映射實(shí)體。在里面<session-factory> </session-factory>
我需要寫:
<mapping class="entity.User"/>
添加回答
舉報(bào)
0/150
提交
取消