我有一个 Web 应用程序,它使用 Hibernate 对数据库进行 CRUD 操作。我收到一条错误消息,指出该表未映射。查看Java文件:
错误信息:
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
method:
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;
...
}
我应该如何修改它才能工作?
异常消息显示:
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
.
事实上,没有。您的映射类型称为Book
。它映射到一个名为的表Books
,但类型称为Book
。当您编写 HQL(或 JPQL)查询时,您使用类型的名称,而不是表。
因此,将您的查询更改为:
select count(*) from Book
虽然我认为可能需要
select count(b) from Book b
如果HQL不支持*
符号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)