Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Exception: org.hibernate.hql.internal.ast.QuerySyntaxException, while retrieving records from table

Tags:

hibernate

hql

In Hibernate 4.0, i want to retrieve record from table using session.createQuery("from dbemployee").list(); but Hibernate is showing exception:

Hibernate Exception: org.hibernate.hql.internal.ast.QuerySyntaxException: dbemployee is not mapped [from dbemployee]**

My POJO class is Employee

public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
private String empId;
private String empName;
private long empSalary;

public Employee() {
    super();
}

// getters and setters

}

My table dbemployee in Oracle 11g is:

dbemployee:
EMPID varchar2(20)
EMPNAME varchar2(20)
EMPSALARY number(11);

Employee.hbm.xml is

<hibernate-mapping>
  <class name="beanclass.Employee" table="dbemployee">

  <id name="empId" type="java.lang.String" column="EMPID">
  <generator class="assigned"></generator>
   </id>
   <property name="empName"  column="EMPNAME"  type="java.lang.String"/>
   <property  name="empSalary" column="EMPSALARY" type="java.lang.Long" />
  </class>

  </hibernate-mapping>

please help to solve this exception. Thanks in advance

like image 243
Vishwas Tyagi Avatar asked Dec 19 '12 14:12

Vishwas Tyagi


2 Answers

Your query should be:

 session.createQuery("from Employee").list();

You have to use the class name in the query, not the table name.

like image 102
Atropo Avatar answered Nov 15 '22 04:11

Atropo


make your query as

 session.createQuery("from Employee").list();

or

session.createQuery("from beanclass.Employee").list();

In an ORM like Hibernate and JPA, when not dealing with native queries you shall use Object/Class names in your queries.

like image 34
Amir Pashazadeh Avatar answered Nov 15 '22 03:11

Amir Pashazadeh