Hibernate table not mapped error in HQL query

后端 未结 9 725
轮回少年
轮回少年 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 13:56

    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!

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

    Hibernate also is picky about the capitalization. By default it's going to be the class name with the First letter Capitalized. So if your class is called FooBar, don't pass "foobar". You have to pass "FooBar" with that exact capitalization for it to work.

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

    Ensure your have set the table name on the entity annotation too.

    @Entity(name = "table_name")
    @Table(name = "table_name")
    public class TableName {
    
    }
    
    0 讨论(0)
  • 2020-11-27 14:07

    In the Spring configuration typo applicationContext.xml where the sessionFactory configured put this property

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="packagesToScan" value="${package.name}"/>
    
    0 讨论(0)
  • 2020-11-27 14:07

    I had same problem , instead @Entity I used following code for getting records

        List<Map<String, Object>> list = null;
                list = incidentHibernateTemplate.execute(new HibernateCallback<List<Map<String, Object>>>() {
    
                @Override
                public List<Map<String, Object>> doInHibernate(Session session) throws HibernateException {
                    Query query = session.createSQLQuery("SELECT * from table where appcode = :app");
                        query.setParameter("app", apptype);
                    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                    return query.list();
    
                    }
                });
    

    I used following code for update

    private @Autowired HibernateTemplate incidentHibernateTemplate;
    Integer updateCount = 0;
    
        updateCount = incidentHibernateTemplate.execute((Session session) -> {
            Query<?> query = session
                        .createSQLQuery("UPDATE  tablename SET key = :apiurl, data_mode = :mode WHERE apiname= :api ");
              query.setParameter("apiurl", url);
              query.setParameter("api", api);
              query.setParameter("mode", mode);
                return query.executeUpdate();
            }
        );
    
    0 讨论(0)
  • 2020-11-27 14:09

    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]
    

    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();
    

    This is how my POJO class is declared:

    @Entity
    @Table(name = "subscriber")
    public class Subscriber
    

    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();
    

    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

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