HQL查询中的休眠表未映射错误 [英] Hibernate table not mapped error in HQL query

查看:87
本文介绍了HQL查询中的休眠表未映射错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,该应用程序使用Hibernate对数据库进行CRUD操作.我收到一条错误消息,说该表未映射.查看Java文件:

I have a web application that use Hibernate to make CRUD operations over a database. I got an error saying that the table is not mapped. See the Java files:

错误消息:

org.springframework.orm.hibernate3.HibernateQueryException: Books is not mapped [SELECT COUNT(*) FROM Books]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Books is not mapped [SELECT COUNT(*) FROM Books]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
...
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Books is not mapped [SELECT COUNT(*) FROM Books]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
...

这是我的DAO.java方法:

public int getTotalBooks(){
    return DataAccessUtils.intResult(hibernateTemplate.find(
          "SELECT COUNT(*) FROM Books"));
}

Book.java:

@Entity
@Table(name="Books")
public class Book {

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    @Column(name="title", nullable=false)
    private String title;
    ...
}

如何修改才能正常工作?

How should I modify it in order to work?

推荐答案

该异常消息说什么?它说:

What does the exception message say? It says:

Books is not mapped [SELECT COUNT(*) FROM Books]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Books is not mapped [SELECT COUNT(*) FROM Books]

这说明了什么?它告诉您Books未映射.也就是说,没有名为Books的映射类型.

What does that tell you? It tell you that Books is not mapped. That is, that there is no mapped type called Books.

实际上,没有.您的映射类型称为Book.它映射到名为Books的表,但类型称为Book.在编写HQL(或JPQL)查询时,您使用的是类型的名称,而不是表.

And indeed, there isn't. Your mapped type is called Book. It's mapped to a table called Books, but the type is called Book. When you write HQL (or JPQL) queries, you use the names of the types, not the tables.

因此,将查询更改为:

select count(*) from Book

尽管我认为可能需要

select count(b) from Book b

如果HQL不支持*表示法.

If HQL doesn't support the * notation.

从阅读异常消息中您可以学到很多东西!

There's a lot you can learn from reading exception messages!

这篇关于HQL查询中的休眠表未映射错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆