Categories: HibernateTutorial

How to solve QuerySyntaxException (table is not mapped) in hibetnate?

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on"><b>The following exception is very common if you are the beginner for hibernate&period;<&sol;b><&sol;div>&NewLine;<pre class&equals;"highlight" name&equals;"code">SLF4J&colon; Class path contains multiple SLF4J bindings&period;&NewLine;SLF4J&colon; Found binding in&NewLine;&lbrack;jar&colon;file&colon;&sol;D&colon;&sol;HIBERNATE&sol;lib&sol;hibernate-annotations-jar-files&sol;slf4j-log4j12-1&period;6&period;1&period;jar&excl;&sol;org&sol;slf4j&sol;impl&sol;StaticLoggerBinder&period;class&rsqb;&NewLine;SLF4J&colon; Found binding in&NewLine;&lbrack;jar&colon;file&colon;&sol;E&colon;&sol;study&percnt;20materials&sol;video&percnt;20tutorials&sol;JAVA&lowbar;J2EE&lowbar;VIDEO&lowbar;TUTORIALS&sol;hibernate&sol;hibernate-annotations-jar-files&sol;slf4j-log4j12-1&period;6&period;1&period;jar&excl;&sol;org&sol;slf4j&sol;impl&sol;StaticLoggerBinder&period;class&rsqb;&NewLine;SLF4J&colon; Found binding in&NewLine;&lbrack;jar&colon;file&colon;&sol;D&colon;&sol;HIBERNATE&sol;lib&sol;slf4j-simple-1&period;5&period;6&period;jar&excl;&sol;org&sol;slf4j&sol;impl&sol;StaticLoggerBinder&period;class&rsqb;&NewLine;SLF4J&colon; See http&colon;&sol;&sol;www&period;slf4j&period;org&sol;codes&period;html&num;multiple&lowbar;bindings for an explanation&period;&NewLine;log4j&colon;WARN No appenders could be found for logger&NewLine;&lpar;org&period;hibernate&period;type&period;BasicTypeRegistry&rpar;&period;&NewLine;log4j&colon;WARN Please initialize the log4j system properly&period;&NewLine;log4j&colon;WARN See http&colon;&sol;&sol;logging&period;apache&period;org&sol;log4j&sol;1&period;2&sol;faq&period;html&num;noconfig&NewLine;for more info&period;&NewLine;Exception in thread "main" org&period;hibernate&period;hql&period;ast&period;QuerySyntaxException&colon;&NewLine;student is not mapped &lbrack;from student&rsqb;&NewLine; at org&period;hibernate&period;hql&period;ast&period;util&period;SessionFactoryHelper&period;requireClassPersister&lpar;SessionFactoryHelper&period;java&colon;180&rpar;&NewLine; at org&period;hibernate&period;hql&period;ast&period;tree&period;FromElementFactory&period;addFromElement&lpar;FromElementFactory&period;java&colon;111&rpar;&NewLine; at org&period;hibernate&period;hql&period;ast&period;tree&period;FromClause&period;addFromElement&lpar;FromClause&period;java&colon;93&rpar;&NewLine; at org&period;hibernate&period;hql&period;ast&period;HqlSqlWalker&period;createFromElement&lpar;HqlSqlWalker&period;java&colon;327&rpar;&NewLine; at org&period;hibernate&period;hql&period;antlr&period;HqlSqlBaseWalker&period;fromElement&lpar;HqlSqlBaseWalker&period;java&colon;3441&rpar;&NewLine; at org&period;hibernate&period;hql&period;antlr&period;HqlSqlBaseWalker&period;fromElementList&lpar;HqlSqlBaseWalker&period;java&colon;3325&rpar;&NewLine; at org&period;hibernate&period;hql&period;antlr&period;HqlSqlBaseWalker&period;fromClause&lpar;HqlSqlBaseWalker&period;java&colon;733&rpar;&NewLine; at org&period;hibernate&period;hql&period;antlr&period;HqlSqlBaseWalker&period;query&lpar;HqlSqlBaseWalker&period;java&colon;584&rpar;&NewLine; at org&period;hibernate&period;hql&period;antlr&period;HqlSqlBaseWalker&period;selectStatement&lpar;HqlSqlBaseWalker&period;java&colon;301&rpar;&NewLine; at org&period;hibernate&period;hql&period;antlr&period;HqlSqlBaseWalker&period;statement&lpar;HqlSqlBaseWalker&period;java&colon;244&rpar;&NewLine; at org&period;hibernate&period;hql&period;ast&period;QueryTranslatorImpl&period;analyze&lpar;QueryTranslatorImpl&period;java&colon;254&rpar;&NewLine; at org&period;hibernate&period;hql&period;ast&period;QueryTranslatorImpl&period;doCompile&lpar;QueryTranslatorImpl&period;java&colon;185&rpar;&NewLine; at org&period;hibernate&period;hql&period;ast&period;QueryTranslatorImpl&period;compile&lpar;QueryTranslatorImpl&period;java&colon;136&rpar;&NewLine; at org&period;hibernate&period;engine&period;query&period;HQLQueryPlan&period;&lt&semi;init&gt&semi;&lpar;HQLQueryPlan&period;java&colon;101&rpar;&NewLine; at org&period;hibernate&period;engine&period;query&period;HQLQueryPlan&period;&lt&semi;init&gt&semi;&lpar;HQLQueryPlan&period;java&colon;80&rpar;&NewLine; at org&period;hibernate&period;engine&period;query&period;QueryPlanCache&period;getHQLQueryPlan&lpar;QueryPlanCache&period;java&colon;124&rpar;&NewLine; at org&period;hibernate&period;impl&period;AbstractSessionImpl&period;getHQLQueryPlan&lpar;AbstractSessionImpl&period;java&colon;156&rpar;&NewLine; at org&period;hibernate&period;impl&period;AbstractSessionImpl&period;createQuery&lpar;AbstractSessionImpl&period;java&colon;135&rpar;&NewLine; at org&period;hibernate&period;impl&period;SessionImpl&period;createQuery&lpar;SessionImpl&period;java&colon;1770&rpar;&NewLine; at kumar&period;saroj&period;ReadStudent&period;main&lpar;ReadStudent&period;java&colon;18&rpar;&NewLine; this is the complete exception hierarchy&period;&NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<&sol;div>&NewLine;<p>The above <i>QuerySyntaxException <&sol;i>is thrown when we are not mapping the table name properly&period; The first time hibernate users<br &sol;>&NewLine;can be seen this error because you will map the table name directly in the query&period; That will not work in the hibernate&period; You will have to<br &sol;>&NewLine;map the class name that is mapped in the Hibernate configuration file&period; Look into the following example&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public static void main&lpar;String&lbrack;&rsqb; args&rpar; &NewLine; &lbrace;&NewLine; &sol;&sol;Create session factory object&NewLine; SessionFactory sessionFactory &equals; HibernateUtil&period;getSessionFactory&lpar;&rpar;&semi;&NewLine; &sol;&sol;getting session object from session factory&NewLine; Session session &equals; sessionFactory&period;openSession&lpar;&rpar;&semi;&NewLine; &sol;&sol;getting transaction object from session object&NewLine; session&period;beginTransaction&lpar;&rpar;&semi;&NewLine; Query query &equals; session&period;createQuery&lpar;"from student"&rpar;&semi;&NewLine; List&lt&semi;Student&gt&semi; students &equals; query&period;list&lpar;&rpar;&semi;&NewLine; for&lpar;Student student &colon; students&rpar;&NewLine; &lbrace;&NewLine;System&period;out&period;println&lpar;"Roll Number&colon; "&plus;student&period;getRollNumber&lpar;&rpar;&plus;"&comma; Student Name&colon; "&plus;student&period;getStudentName&lpar;&rpar;&plus;"&comma; Course&colon; "&plus;student&period;getCourse&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; session&period;getTransaction&lpar;&rpar;&period;commit&lpar;&rpar;&semi;&NewLine; sessionFactory&period;close&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>&NewLine;The above code give exception mention in above&period;<&sol;p>&NewLine;<p><b>Description&colon;<&sol;b><&sol;p>&NewLine;<p>Your query is not a SQL query&period; It&&num;8217&semi;s a HQL query&period; It thus should not use table names&comma; but entity class names &lpar;from Student instead of from student&rpar;&period; <&sol;p>&NewLine;<p> In above code we using following line&period; Here &&num;8220&semi;student&&num;8221&semi; is not entity name by default entity name is same as class name so we have use &&num;8220&semi;Student&&num;8221&semi; instead of &&num;8220&semi;student&&num;8221&semi; because of HQL is pick entity name not table name&lpar;table name is &&num;8220&semi;student&&num;8221&semi;&rpar;&period; So please we care about this concern&period; <&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">Query query &equals; session&period;createQuery&lpar;"from student"&rpar;&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>If you using the following code then you got successfully running&period; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public static void main&lpar;String&lbrack;&rsqb; args&rpar; &NewLine; &lbrace;&NewLine; &sol;&sol;Create session factory object&NewLine; SessionFactory sessionFactory &equals; HibernateUtil&period;getSessionFactory&lpar;&rpar;&semi;&NewLine; &sol;&sol;getting session object from session factory&NewLine; Session session &equals; sessionFactory&period;openSession&lpar;&rpar;&semi;&NewLine; &sol;&sol;getting transaction object from session object&NewLine; session&period;beginTransaction&lpar;&rpar;&semi;&NewLine; Query query &equals; session&period;createQuery&lpar;"from Student"&rpar;&semi;&NewLine; List&lt&semi;Student&gt&semi; students &equals; query&period;list&lpar;&rpar;&semi;&NewLine; for&lpar;Student student &colon; students&rpar;&NewLine; &lbrace;&NewLine;System&period;out&period;println&lpar;"Roll Number&colon; "&plus;student&period;getRollNumber&lpar;&rpar;&plus;"&comma; Student Name&colon; "&plus;student&period;getStudentName&lpar;&rpar;&plus;"&comma; Course&colon; "&plus;student&period;getCourse&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; session&period;getTransaction&lpar;&rpar;&period;commit&lpar;&rpar;&semi;&NewLine; sessionFactory&period;close&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;<&sol;pre>&NewLine;<p><b>Here the class names is case sensitive&period;<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;get-http-header-in-jax-rs&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-search-api-hibernate-4-on&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '405'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago