Hibernate Table Not Mapped Error in Hql Query

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:

  1. either you don't have Payment listed in your hibernat.cfg.xml or where ever you config your mapped classes.

  2. another reason might be the confusion between javax...Entity and org.hibernate....Entity. Make sure you use the first one.



Related Topics



Leave a reply



Submit