HomeHibernateorg.hibernate.hql.internal.ast.querysyntaxexception entity/table is not mapped

org.hibernate.hql.internal.ast.querysyntaxexception entity/table is not mapped

org.hibernate.hql.internal.ast.querysyntaxexception: entity is not mapped [from entity] – this is a common error I used to get when working with Hibernate queries in JPA with Hibernate or with Spring Applications.

1. Exception Stack

Following is the Exception Stack occurred while creating Hibernate query.

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Student is not mapped [From Student]
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:718)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:109)
	at com.javabydeveloper.App.main(App.java:28)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Student is not mapped [From Student]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
	at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155)
	at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:600)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:709)
	... 2 more

To understand where it went wrong with mapping, let’s have a quick look into data base table and Entity class and the Hibernate Query.

2. Data Base Table and Entity mapping

Hibernate Query :

 Query<Student> q = session.createQuery("From Student");
 List<Student> resultList = q.list();

Entity :

@Entity(name = "STUDENT")
public class Student {

	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
	@GenericGenerator(name = "native", strategy = "native")
	@Column(name = "ID")
	private Long studentId;

	@Column(name = "FNAME")
	private String firstName;

	@Column(name = "LNAME")
	private String lastName;

	@Column(name = "CONTACT_NO")
	private String contactNo;

       // Setters and Getters

Database Table :

create table STUDENT (
       ID bigint not null auto_increment,
        CONTACT_NO varchar(255),
        FNAME varchar(255),
        LNAME varchar(255),
        primary key (ID)

3. Common Mistakes

Have a quick look on following image for correct Entity mapping, that also resolves the problem.

org.hibernate.hql.internal.ast.querysyntaxexception is not mapped

3.1. If your Table name and Entity name is same following are the correct mappings for create Hibernate query session.createQuery("From Student");

public class Student {
    // fields

3.2. @Table annotation can be used only when your Entity class name and and database table names are not same.

public class Student {
    // fields

3.3. If you would like to use specific name for your entity (for example short names) in Hibernate queries, mapping should be like following

@Entity(name = "stdnt")
@Table(name = "STUDENT")
public class Student {
    // fields
Query<Student> q = session.createQuery("From stdnt");


Query<Student> q = session.createQuery("From stdnt", Student.class);

4. Conclusion

Specifying table name in @Entity mistake is one of the most common reason that causes for Exception org.hibernate.hql.internal.ast.querysyntaxexception: entity is not mapped [from entity] when you are working with Hibernate queries in JPA with Hibernate or with Spring Applications.

1.1. Used Technologies :

  1. hibernate-core-5.4.18.Final
  2. Maven 3+
  3. Mysql 8

You can checkout source code from our github repository for testing.

5. References

  1. Hibernate doc
  2. Hibernate 5 Example
  3. Hibernate Dialect
  4. JPA Mysql example



Please enter your comment!
Please enter your name here