休眠错误 \- 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-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;
    }
    ...
}

知道为什么我会收到此错误吗?

原文由 Blue 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 527
2 个回答

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

 List<User> result = session.createQuery("from User", User.class).getResultList();


更新:更准确地说,您应该使用 @Entity 中配置的实体名称来引用“表”,如果您没有明确设置,则默认为映射 java 类 的非限定名称。

(PS是 @javax.persistence.Entity 但不是 @org.hibernate.annotations.Entity

原文由 Ken Chan 发布,翻译遵循 CC BY-SA 4.0 许可协议

例如:你的bean类名是 UserDetails

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

您不在 Db 上提供表名。 你给 bean 的类名

原文由 Kumaresan Perumal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题