Hibernate table not mapped error in HQL query

后端 未结 9 766
轮回少年
轮回少年 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!

提交回复
热议问题