hibernate 报错 **** is not mapped 【from ****】

我在使用hibernate的Query对象查询数据列表时,遇到如下报错:

ERROR [org.lujx.test.ManageCountry] - 查询信息失败 ,异常信息 :t_country is not mapped [from t_country]

我的数据表结构如下:

+------------+-------------+------+-----+---------+----------------+
| Field           | Type            | Null   | Key  | Default  | Extra                 |
+------------+-------------+------+-----+---------+-----------------+
| c_id             | int(11)         | NO    | PRI   | NULL    |auto_increment |
| c_name       | varchar(20)  | YES   |         | NULL     |                          |
| c_location   | varchar(50)  | YES   |         | NULL     |                          |

+------------+-------------+------+-----+---------+-----------------+

hibernate配置文件如下 

<hibernate-mapping package="org.lujx.model">
<class name=" Country"  table=" t_country"  catalog="lujx">
<id name="id" column="c_id">
<generator class="native"></generator>
</id>
<property name="name" column="c_name" length="20"></property>
<property name="location" column="c_location" length="50"></property>
</class>

</hibernate-mapping>

查询代码如下:

        List<Country> countries = new ArrayList<Country>();
try {
Query query = session.createQuery("from t_country");
countries = query.list();
print(countries);
} catch (HibernateException e) {
LOGGER.error("查询信息失败 ,异常信息 :" + e.getMessage());
}finally {
session.close();
sessionFactory.close();
}

出现这个错误的根本原因是hql语法里面是POJO对象而不是table,所以应该将createQuery 参数修改为配置文件中所要查询的表名所映射的java实体类名,在此例中,正确的写法为:

Query query = session.createQuery("from Country");

ps:参数对大小写敏感,所以一定要注意大小写。

特此记录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值