Hibernate表未在HQL查询中映射错误

Pas*_*cut 57 java eclipse spring hibernate hibernate-mapping

我有一个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)
...
Run Code Online (Sandbox Code Playgroud)

这是我的DAO.java方法:

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

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;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我应该如何修改才能工作?

Tom*_*son 110

异常消息说什么?它说:

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不支持*表示法.

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

  • 这里的关键点是:在您的HQL中,您必须使用实体名称而不是表名.这一切都有所不同. (32认同)
  • 答案很好,但可以放任自高。 (6认同)
  • 我看到了错误.你明白我的答案了吗? (4认同)
  • 当然它说它没有被映射但是错误并且你没有说如何进行映射。 (3认同)

Gra*_*ray 17

hibernate3.HibernateQueryException: Books is not mapped [SELECT COUNT(*) FROM Books];

Hibernate试图说它不知道一个名为"Books"的实体.让我们来看看你的实体:

@javax.persistence.Entity
@javax.persistence.Table(name = "Books")
public class Book {
Run Code Online (Sandbox Code Playgroud)

对.该的名称Book已更名为"书籍",但实体名称还是从类名"书".如果要设置实体名称,则应使用@Entity注释的名称:

// this allows you to use the entity Books in HQL queries
@javax.persistence.Entity(name = "Books")
public class Book {
Run Code Online (Sandbox Code Playgroud)

它设置实体名称和表名称.


当我从Person.hbm.xml文件迁移到使用Java注释来描述hibernate字段时,我遇到了相反的问题.我的旧XML文件有:

<hibernate-mapping package="...">
    <class name="Person" table="persons" lazy="true">
       ...
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

我的新实体有一个@Entity(name=...)我需要设置表的名称.

// this renames the entity and sets the table name
@javax.persistence.Entity(name = "persons")
public class Person {
    ...
Run Code Online (Sandbox Code Playgroud)

我当时看到的是HQL错误,如:

QuerySyntaxException: Person is not mapped
     [SELECT id FROM Person WHERE id in (:ids)]
Run Code Online (Sandbox Code Playgroud)

这个问题是实体名称也被重命名persons.我应该使用以下方法设置表名:

// no name = here so the entity can be used as Person
@javax.persistence.Entity
// table name specified here
@javax.persistence.Table(name = "persons")
public class Person extends BaseGeneratedId {
Run Code Online (Sandbox Code Playgroud)

希望这有助于其他人.


Sib*_*gha 12

This answer comes late but summarizes the concept involved in the "table not mapped" exception(in order to help those who come across this problem since its very common for hibernate newbies). This error can appear due to many reasons but the target is to address the most common one that is faced by a number of novice hibernate developers to save them hours of research. I am using my own example for a simple demonstration below.

The exception:

org.hibernate.hql.internal.ast.QuerySyntaxException: subscriber is not mapped [ from subscriber]
Run Code Online (Sandbox Code Playgroud)

In simple words, this very usual exception only tells that the query is wrong in the below code.

Session session = this.sessionFactory.getCurrentSession();
List<Subscriber> personsList = session.createQuery(" from subscriber").list();
Run Code Online (Sandbox Code Playgroud)

This is how my POJO class is declared:

@Entity
@Table(name = "subscriber")
public class Subscriber
Run Code Online (Sandbox Code Playgroud)

But the query syntax "from subscriber" is correct and the table subscriber exists. Which brings me to a key point:

  • It is an HQL query not SQL.

and how its explained here

HQL works with persistent objects and their properties not with the database tables and columns.

Since the above query is an HQL one, the subscriber is supposed to be an entity name not a table name. Since I have my table subscriber mapped with the entity Subscriber. My problem solves if I change the code to this:

Session session = this.sessionFactory.getCurrentSession();
List<Subscriber> personsList = session.createQuery(" from Subscriber").list();
Run Code Online (Sandbox Code Playgroud)

Just to keep you from getting confused. Please note that HQL is case sensitive in a number of cases. Otherwise it would have worked in my case.

Keywords like SELECT , FROM and WHERE etc. are not case sensitive but properties like table and column names are case sensitive in HQL.

https://www.tutorialspoint.com/hibernate/hibernate_query_language.htm

To further understand how hibernate mapping works, please read this