score:150
The exception message 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]
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.
score:0
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.
score:0
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();
}
);
score:1
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}"/>
score:2
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.
score:3
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!
score:4
Ensure your have the Entity annotation and Table annotation and set the table name on the table annotation.
@Entity
@Table(name = "table_name")
public class TableName {
}
score:15
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
score:24
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 {
Source: stackoverflow.com
Related Query
- Hibernate table not mapped error in HQL query
- hibernate not creating table but no error messages
- Hibernate error - QuerySyntaxException: administrator is not mapped error
- Why is this table not found error using SparkSql with Hive?
- Column (XIT115) not found in any table in the query (or SLV is undefined)
- JPA query not in subquery relationship table
- Updating a table using 'Eclipse HQL Editor' gives error
- Hibernate and Glassfish : Class is not mapped
- I have an incorrect Query but I am not able to find the error
- Android 'create table if not exist' error in Eclipse
- Executing multiline query does not work with table alias where single-line query does
- Hibernate not finding book to create table
- Hibernate : Class not found error (java.lang.NoClassDefFoundError), although dependency added in Maven
- hibernate Table name pattern can not be NULL or empty
- Error: "reading schema error: error calling driver#connect" when configuring Table filters in the Hibernate Reverse Engineering File (reveng.xml)
- Java Eclipse : Hibernate Configuration to AnnotationConfiguration does not work and gives the runtime error
- Hibernate Tools in Eclipse: Change in column values does not reflecting in the query result
- Hibernate autogenerated SQL table is not being created
- Eclipse Hibernate configurations pane does not display new Table
- How to solve could not create the virtual machine error of Java Virtual Machine Launcher?
- File not recognized: File truncated GCC error
- Eclipse could not delete error
- Java parsing XML document gives "Content not allowed in prolog." error
- "string could not resolved" error in Eclipse for C++ (Eclipse can't resolve standard library)
- "Source folder is not a Java project" error in eclipse
- ERROR StatusLogger Log4j2 could not find a logging implementation
- Eclipse on Mac, getting "Specified VM install not found" error when trying to build
- Error launching Eclipse 4.4 "Version 1.6.0_65 of the JVM is not suitable for this product."
- Class "model.Address" is listed in the persistence.xml file but not mapped
- Eclipse c++ Type could not be resolved error even though build is successful
More Query from same tag
- In Eclipse/EGit how can I see unpushed commits?
- Eclipse - JUnit5 Source not found - How to add JUnit5 source code?
- Hadoop 2: Setting up Hadoop 2 code in eclipse to modify source code and run/test?
- Stopping eclipse LogCat to receive logs
- Find references of an Interface across the workspace programatically
- Getting Unable to locate element: error when executing code through POM, The same locator works fine when executed through single main() program
- Java Sound Recorder, doesn't work
- Android emulator for eclipse on Windows Server 2008
- After using eclipse from adt, error creating pure java project, on mac
- Getting Feature from Resource
- Does anyone else have this showing in eclipse?
- Using org.eclipse.core.expressions: expressionLanguage.exsd could not be found
- How to resolve The method addView(View, int, LinearLayout.LayoutParams) of type YourLayout must override or implement a supertype method
- Eclipse 4.3.2 with Java 8 patches doesn't recognize source level 1.8
- I think I've overridden a virtual method but I'm still getting: "X must implement the inherited pure virtual method Y"
- Thread continues to run after run() method
- Why is my Google App Engine run options missing in eclipse..?
- Extract Wikipedia Infobox data
- java android R cannot be resolved to a variable error
- How to hide status bar - Android
- Migrate Eclipse project to Gradle
- How to get eclipse workspace path
- hibernate.cfg.xml not found on the JBoss server on OpenShift
- OpenFrameworks Doesn't Compile with c++11, GPGS needs c++11
- Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)
- How to debug a play framework 2.2.5 application in eclipse ide?
- CLabel windows builder image issue
- Bug in Eclipse? org.eclipse.jdt.internal.compiler.lookup.ArrayBinding cannot be cast to org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding
- Receiving "bad symbolic reference to org.json4s.JsonAST..." in Eclipse Scala-IDE using 2.11
- All warnings as errors with the Eclipse Java compiler (3.6.2)