gpt4 book ai didi

java - org.hibernate.hql.internal.ast.QuerySyntaxException : Hibernate not able to map table name

转载 作者:太空宇宙 更新时间:2023-11-04 11:59:17 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于java - org.hibernate.hql.internal.ast.QuerySyntaxException : Hibernate not able to map table name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41080696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com