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

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

问题描述

我有一个 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?

推荐答案

异常信息说:

图书未映射 [SELECT COUNT(*) FROM Books];嵌套异常是 org.hibernate.hql.ast.QuerySyntaxException:书籍未映射 [SELECT COUNT(*) FROM Books]

Books 未映射.也就是说,没有名为 Books 的映射类型.

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.

因此,将您的查询更改为:

So, change your query to:

select count(*) from Book

虽然我认为可能需要

从Book b中选择count(b)

如果 HQL 不支持 * 符号.

If HQL doesn't support the * notation.

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

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