问题描述
我有一个使用Hibernate的Web应用程序来对数据库进行CRUD操作。我收到一个错误,表示该表未映射。请参阅Java文件:错误消息:
org.springframework .orm.hibernate3.HibernateQueryException:图书没有映射[SELECT COUNT(*)FROM Books];嵌套异常是org.hibernate.hql.ast.QuerySyntaxException:图书没有映射[SELECT COUNT(*)FROM Books]
在org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
在org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
在org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
。 ..
导致:org.hibernate.hql.ast.QuerySyntaxException:图书没有映射[SELECT COUNT(*)FROM Books]
在org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister (SessionFactoryHelper.java:181)
在org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
在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;
...
}
为了工作,我应该如何修改?
异常消息是什么?它说:
这告诉你什么?它告诉你,书籍
没有映射。也就是说,没有映射类型称为 Books
。
确实没有。您的映射类型称为书
。它映射到一个名为 Books
的表,但该类型称为 Book
。当您编写HQL(或JPQL)查询时,您可以使用类型的名称,而不是表。
因此,将查询更改为:
虽然我认为可能需要
如果HQL不支持 *
符号。
您可以从阅读异常消息中学到很多东西!
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:
Error message:
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)
...
Here's my 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;
...
}
How should I modify it in order to work?
What does the exception message say? It says:
What does that tell you? It tell you that Books
is not mapped. That is, that there is no mapped type called Books
.
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:
Although i think it may need to be
If HQL doesn't support the *
notation.
There's a lot you can learn from reading exception messages!
这篇关于Hibernate表未映射错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!