Hibernate table not mapped error in HQL query

后端 未结 9 726
轮回少年
轮回少年 2020-11-27 13:47

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 me

相关标签:
9条回答
  • 2020-11-27 14:09

    Thanks everybody. Lots of good ideas. This is my first application in Spring and Hibernate.. so a little more patience when dealing with "novices" like me..

    Please read Tom Anderson and Roman C.'s answers. They explained very well the problem. And all of you helped me.I replaced

    SELECT COUNT(*) FROM Books
    

    with

    select count(book.id) from Book book
    

    And of course, I have this Spring config:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="packagesToScan" value="extjs.model"/>
    

    Thank you all again!

    0 讨论(0)
  • 2020-11-27 14:11

    In addition to the accepted answer, one other check is to make sure that you have the right reference to your entity package in sessionFactory.setPackagesToScan(...) while setting up your session factory.

    0 讨论(0)
  • 2020-11-27 14:19

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

    Hibernate is trying to say that it does not know an entity named "Books". Let's look at your entity:

    @javax.persistence.Entity
    @javax.persistence.Table(name = "Books")
    public class Book {
    

    Right. The table name for Book has been renamed to "Books" but the entity name is still "Book" from the class name. If you want to set the entity name, you should use the @Entity annotation's name instead:

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

    That sets both the entity name and the table name.


    The opposite problem happened to me when I was migrating from the Person.hbm.xml file to using the Java annotations to describe the hibernate fields. My old XML file had:

    <hibernate-mapping package="...">
        <class name="Person" table="persons" lazy="true">
           ...
    </hibernate-mapping>
    

    And my new entity had a @Entity(name=...) which I needed to set the name of the table.

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

    What I then was seeing was HQL errors like:

    QuerySyntaxException: Person is not mapped
         [SELECT id FROM Person WHERE id in (:ids)]
    

    The problem with this was that the entity name was being renamed to persons as well. I should have set the table name using:

    // 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 {
    

    Hope this helps others.

    0 讨论(0)
提交回复
热议问题