org.hibernate.hql.internal.ast.QuerySyntaxException:Hibernate无法映射表名

时间:2016-12-10 21:51:28

标签: java spring hibernate

我是Hibernate的新手,我收到此错误: org.hibernate.hql.internal.ast.QuerySyntaxException:未映射用户。 在做了一些研究之后,我发现由于HQL要求表格大写,我已经完成了,如下面的代码所示。

public class UserLogin {

        @GET
        @Path("/login")
        @Produces(MediaType.APPLICATION_JSON)
        public User login(@QueryParam("username") String username, @QueryParam("password") String password) {
            String localUser;
            String localPass;
            List allUsers;
            User user = new User();

            if (username == null || password == null) {
                user.setReturnComment("Null values sent");
                return user;
            }

            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml");

            SessionFactory factory = cfg.buildSessionFactory();
            Session session = factory.openSession();
            UserDao userDao = new UserDao();

            /*userDao.setUsername(username);
            userDao.setPassword(password);*/

            Transaction tx = session.beginTransaction();
            //session.save(userDao);


            Query queryResult = session.createQuery("from Users");
            allUsers = queryResult.getResultList();

            tx.commit();
            session.close();
            factory.close();

            return user;
    }
}

这是我的hbm.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="dao.UserDao" table="users">
        <id column="id" name="Id" type="int">
            <generator class="increment"></generator>
        </id>
        <property column="username" name="Username" type="string" />
        <property column="password" name="Password" type="string" />
    </class>
</hibernate-mapping>

无法理解我做错了什么。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在HQL中,您应该使用HBM配置中提到的类名,因此请将查询更改为

Query queryResult = session.createQuery("from UserDao");