2012-03-31 45 views
87

我试图让所有从“用户”表中的用户的列表,并且我收到以下错误:休眠错误 - 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(); 
} 

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

这是我的hibernate配置文件:

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

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

+1

相关:http://stackoverflow.com/questions/14446048/hibernate-table-not-mapped-error – Gray 2016-07-20 15:48:12

回答

211

在HQL,你应该使用Java类名和映射@Entity而不是实际的表名和列名的属性名称,所以HQL应该是:

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

所以实际上这甚至是大小写敏感的。所以“从用户”将导致相同的异常 – tObi 2014-07-11 12:25:46

+1

它是答案的一部分,但要解决这个错误,我不得不使用像这样的完整的对象路径:[...]。createQuery(“from gcv.metier.User作为你在哪里u.username =?“) – Raphael 2015-10-02 00:13:56

11

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

import org.hibernate.annotations.Entity; 

import javax.persistence.Entity; 
+0

它为我工作。 – PKTomar 2017-05-20 13:00:26

7

新增@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; 
    } 
} 
+0

请考虑削减您的代码示例。 – Gray 2016-07-20 15:44:13

4

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

+0

我也忘了在hibernate.cfg.xml中添加POJO类。 – Chinmoy 2018-02-27 06:09:17

13

例如:你的bean类名称为的UserDetails

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

您不要在数据库给你的表名。 给你豆类名称

2

我也被导入错误的实体import org.hibernate.annotations.Entity; 它应该是进口javax.persistence.Entity;

5

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 table not mapped error

3

也检查你使用添加了注解类:

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

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

5

还要确保以下属性在Hibernate bean的配置设置:

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

这告诉spring和hibernate在哪里可以找到注释为实体域类。

5

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

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

你确定“基于Linux的MySQL”在这里有作用吗? – asgs 2017-11-23 20:32:59

0

我在用hibernate-core-5.2.12替换旧的hibernate-core库时遇到了这个问题。然而,我所有的配置都可以。我创建的SessionFactory这种方式修复了这个问题:

private static SessionFactory buildsSessionFactory() { 
    try { 
     if (sessionFactory == null) { 
      StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() 
        .configure("/hibernate.cfg.xml").build(); 
      Metadata metaData = new MetadataSources(standardRegistry) 
        .getMetadataBuilder().build(); 
      sessionFactory = metaData.getSessionFactoryBuilder().build(); 
     } 
     return sessionFactory; 
    } catch (Throwable th) { 

     System.err.println("Enitial SessionFactory creation failed" + th); 

     throw new ExceptionInInitializerError(th); 

    } 
} 

希望它可以帮助别人