Hibernate错误 – QuerySyntaxException:用户未映射

我试图从“用户”表中获得所有用户的列表,我得到以下错误:

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) 

这是我写的添加/获取用户的代码:

 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(); } 

添加用户的作品,但是当我使用getUsersfunction,我得到这些错误。

这是我的hibernateconfiguration文件:

 <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> 

这是我的用户类:

 @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; } ... } 

任何想法,为什么我得到这个错误?

在HQL中,您应该使用映射的@Entity的java类名称和属性名称,而不是实际的表名称和列名称,因此HQL应该是:

 List<User> result = (List<User>) session.createQuery("from User").list(); 

例如:你的bean类的名字是UserDetails

 Query query = entityManager. createQuery("Select UserName from **UserDetails** "); 

你不要在Db上给你的表名。 给你豆类的名字

只是为了分享我的发现。 即使查询的目标是正确的类名,我仍然得到相同的错误。 后来我意识到我是从错误的包中导入实体类。

我更改导入行后,问题就解决了:

 import org.hibernate.annotations.Entity; 

 import javax.persistence.Entity; 

添加了@TABLE(name = "TABLE_NAME")注释并进行了修复。 检查你的注释和hibernate.cfg.xml文件。 这是工作的示例实体文件:

 import javax.persistence.*; @Entity @Table(name = "VENDOR") public class Vendor { //~ --- [INSTANCE FIELDS] ------------------------------------------------------------------------------------------ private int id; private String name; //~ --- [METHODS] -------------------------------------------------------------------------------------------------- @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final Vendor vendor = (Vendor) o; if (id != vendor.id) { return false; } if (name != null ? !name.equals(vendor.name) : vendor.name != null) { return false; } return true; } //~ ---------------------------------------------------------------------------------------------------------------- @Column(name = "ID") @GeneratedValue(strategy = GenerationType.AUTO) @Id public int getId() { return id; } @Basic @Column(name = "NAME") public String getName() { return name; } public void setId(final int id) { this.id = id; } public void setName(final String name) { this.name = name; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0); return result; } } 

一些基于Linux的MySQL安装需要区分大小写。 解决方法是应用nativeQuery

 @Query(value = 'select ID, CLUMN2, CLUMN3 FROM VENDOR c where c.ID = :ID', nativeQuery = true) 

还要确保在你的hibernate beanconfiguration中设置了以下属性:

 <property name="packagesToScan" value="yourpackage" /> 

这告诉spring和冬眠在哪里可以find你的域名注释为实体。

有可能你忘记添加到创build的实体映射到hibernate.cfg.xml ,同样的错误。

org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users]

这表明hibernate不知道User实体为“用户”。

 @javax.persistence.Entity @javax.persistence.Table(name = "Users") public class User { 

@Table注释将名设置为“用户”,但实体名在HQL中仍被称为“用户”。

要改变这两者,你应该设置实体的名称:

 // this sets the name of the table and the name of the entity @javax.persistence.Entity(name = "Users") public class User implements Serializable{ 

有关更多信息,请参阅我的答案: Hibernate表未映射错误

另外检查你是否使用以下方法添加了带注释的类:

new Configuration().configure("configuration file path").addAnnotatedClass(User.class)

当使用Hibernate在数据库中添加新表时,总是浪费我的时间。

我还导入了错误的实体import org.hibernate.annotations.Entity; 它应该是导入javax.persistence.Entity;