Hibernate错误 - QuerySyntaxException:用户未映射[来自用户] [英] Hibernate error - QuerySyntaxException: users is not mapped [from users]
问题描述
我试图从users表中获取所有用户的列表,并收到以下错误消息: > org.hibernate.hql.internal.ast.QuerySyntaxException:用户未映射[来自用户]
org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
这是我写的添加/获取用户的代码:
public List< User> getUsers(){
Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();
列表<用户> result =(List< User>)session.createQuery(from users)。list();
session.getTransaction()。commit();
返回结果;
}
public void addUser(User user){
Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction()。commit();
$ b $ public void addUser(List< User> users){
Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction(); (用户用户:用户)
{
session.save(user);
}
session.getTransaction()。commit();
}
添加用户工作,但是当我使用getUsers函数时,我得到这些错误。
这是我的hibernate配置文件:
< hibernate-结构>
< session-factory>
< property name =connection.url> jdbc:mysql:// localhost:3306 / test< / property>
< property name =connection.username> root< / property>
< property name =connection.password> root< / property>
< property name =connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =hibernate.default_schema> test< / property>
< property name =dialect> org.hibernate.dialect.MySQL5Dialect< / property>
< property name =show_sql> true< / property>
< property name =format_sql> true< / property>
< property name =hbm2ddl.auto> create-drop< / property>
<! - JDBC连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>
< property name =current_session_context_class>线程< / property>
<! - 映射文件将在此处显示.... - >
< mapping class =model.Company/>
< mapping class =model.Conference/>
< mapping class =model.ConferencesParticipants/>
< mapping class =model.ConferenceParticipantStatus/>
< mapping class =model.ConferencesUsers/>
< mapping class =model.Location/>
< mapping class =model.User/>
< / session-factory>
这是我的User类:
$ b $ pre $ @Entity
@Table(name =Users)
public class User实现Serializable {
私人长用户ID;
private int pasportID;
私人公司公司;
私人字符串名称;
私人字符串电子邮件;
private String phone1;
private String phone2;
私人字符串密码; //可以为空/空,将保持散列
private boolean isAdmin;
私人日期lastLogin;
用户(){} //不公开目的!
$ b $ public User(int countryID,Company company,String name,String email,
String phone1,String phone2,String password,boolean isAdmin){
this.pasportID = countryID;
this.company = company;
this.name = name;
this.email =电子邮件;
this.phone1 = phone1;
this.phone2 = phone2;
this.password =密码;
this.isAdmin = isAdmin;
$ b $ @ @
@GeneratedValue(generator =increment)
@GenericGenerator(name =increment,strategy =increment)
public long getUserID(){
return userID;
}
public void setUserID(long userID){
this.userID = userID;
}
...
}
得到这个错误?
列表<使用者> result =(List< User>)session.createQuery(from User).list();
I'm trying to get a list of all the users from "users" table and I get the following error:
org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users]
org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
This is the code I wrote to add/get users:
public List<User> getUsers() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<User> result = (List<User>) session.createQuery("from users").list();
session.getTransaction().commit();
return result;
}
public void addUser(User user) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
public void addUser(List<User> users) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
for (User user : users) {
session.save(user);
}
session.getTransaction().commit();
}
Adding users works, but when I use the getUsers function I get these error.
This is my hibernate config file:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.default_schema">test</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<!-- Mapping files will go here.... -->
<mapping class="model.Company" />
<mapping class="model.Conference" />
<mapping class="model.ConferencesParticipants" />
<mapping class="model.ConferenceParticipantStatus" />
<mapping class="model.ConferencesUsers" />
<mapping class="model.Location" />
<mapping class="model.User" />
</session-factory>
and this is my User class:
@Entity
@Table( name = "Users" )
public class User implements Serializable{
private long userID;
private int pasportID;
private Company company;
private String name;
private String email;
private String phone1;
private String phone2;
private String password; //may be null/empty , will be kept hashed
private boolean isAdmin;
private Date lastLogin;
User() {} //not public on purpose!
public User(int countryID, Company company, String name, String email,
String phone1, String phone2, String password, boolean isAdmin) {
this.pasportID = countryID;
this.company = company;
this.name = name;
this.email = email;
this.phone1 = phone1;
this.phone2 = phone2;
this.password = password;
this.isAdmin = isAdmin;
}
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
...
}
Any idea why I get this error?
In the HQL , you should use the java class name and property name of the mapped @Entity
instead of the actual table name and column name , so the HQL should be :
List<User> result = (List<User>) session.createQuery("from User").list();
这篇关于Hibernate错误 - QuerySyntaxException:用户未映射[来自用户]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!