Hibernate表未映射错误 [英] Hibernate table not mapped error

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

问题描述

我有一个使用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;
...
}

为了工作,我应该如何修改?

解决方案

异常消息是什么?它说:


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


这告诉你什么?它告诉你,书籍没有映射。也就是说,没有映射类型称为 Books



确实没有。您的映射类型称为。它映射到一个名为 Books 的表,但该类型称为 Book 。当您编写HQL(或JPQL)查询时,您可以使用类型的名称,而不是表。



因此,将查询更改为:


从书中选择计数(*)


虽然我认为可能需要


select count(b)从书b


如果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:

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

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:

select count(*) from Book

Although i think it may need to be

select count(b) from Book b

If HQL doesn't support the * notation.

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

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

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