Hibernate table not mapped error in HQL query
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.
org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped
Finally I found a mistake! Hope this is useful to someone. When doing a request to the database(in my case it Apache Derby), name of base need write the first letter upper case other in lower case.
This is wrong query:
session.createQuery("select first_name from CUSTOMERV").
This is valid query
session.createQuery("select first_name from Customerv").
And class entity must be same name as database, but I'm not sure.
Hibernate table not mapped error in query
After reading your configurations, you have misconfigured your EntityManager. You have this:
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.MYSQL);
adapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(getDataSource());
bean.setJpaVendorAdapter(adapter);
bean.setPackagesToScan(getClass().getPackage().getName()); // This line scan the package of your configuration class not the package containing your entities
return bean;
}
You should have something like:
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.MYSQL);
adapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(getDataSource());
bean.setJpaVendorAdapter(adapter);
bean.setPackagesToScan(Prodotto.class.getPackageName);
return bean;
}
Also, your configurations could be improved.
Hibernate error - QuerySyntaxException: users is not mapped [from users]
In the HQL , you should use the java class name and property name of the mapped @Entity
instead of the actual table name and column name , so the HQL should be :
List<User> result = session.createQuery("from User", User.class).getResultList();
Update : To be more precise , you should use the entity name configured in @Entity
to refer to the "table" , which default to unqualified name of the mapped java class if you do not set it explicitly.
(P.S. It is @javax.persistence.Entity
but not @org.hibernate.annotations.Entity
)
Hibernate is not mapped
"from songs"
is actually HQL (HIbernate Query Language) so you should be using the OBJECT NAME that corresponds to your Table.
In this case, try "from Song"
Hibernate throws strange error: Class is not mapped
I'd expect one of two things to be the reason:
either you don't have Payment listed in your hibernat.cfg.xml or where ever you config your mapped classes.
another reason might be the confusion between javax...Entity and org.hibernate....Entity. Make sure you use the first one.
Related Topics
Find Duplicate Element in Array in Time O(N)
How Do Java Method Annotations Work in Conjunction with Method Overriding
Why Is My Jlabel Not Showing Up
How Much Memory Does a String Use in Java 8
Annotations from Javax.Validation.Constraints Not Working
Java Enum Methods - Return Opposite Direction Enum
Making a Log4J Console Appender Use Different Colors for Different Threads
Refreshing Static Content with Spring MVC and Boot
Java: Subtract '0' from Char to Get an Int... Why Does This Work
Comparing Boxed Long Values 127 and 128
When Not to Use the Static Keyword in Java
Unsatisfieddependencyexception: Error Creating Bean with Name
Is There Any Good and Free Date and Time Picker Available for Java Swing
Steps Needed to Use MySQL Database with Play Framework 2.0