0

I am getting Error like:

org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]",
    "trace": "org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee];

I have created the Modal like given below

@Entity
@Table(name="employee_list")
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private Integer id;

    @Column
    private String name;

    @Column
    private String gender;

    @Column
    private String department;

    @Column
    private Date dob;
    // getters/setters
}

My Dao Implementation:

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<Employee> get() {
        Session currentSession = entityManager.unwrap(Session.class);
        Query<Employee> query =  currentSession.createQuery("from Employee", Employee.class);
        List<Employee> list = query.getResultList();
        return list;
    }
}

I am missing something.
I am not able to identify what exactly.

catch23
  • 17,519
  • 42
  • 144
  • 217
Krishna
  • 1,332
  • 5
  • 20
  • 35

3 Answers3

1

HQL should be like following:

currentSession.createQuery("select e from Employee e", Employee.class);

Also, you could use Criteria API:

currentSession.createCriteria(Employee.class).list();

Useful references:

catch23
  • 17,519
  • 42
  • 144
  • 217
  • I am sill getting the error, org.hibernate.hql.internal.ast.QuerySyntaxException: employee_list is not mapped [from employee_list]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: employee_list is not mapped [from employee_list]", "trace": "org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: employee_list is not mapped [from employee_list]; nested exception is java.lang.IllegalArgumentException – Krishna Apr 26 '20 at 19:54
  • Criteria APi, seems to be deprecated – Krishna Apr 26 '20 at 19:54
  • @Krishna misunderstood I suppose it was a native query. HQL doesn't use table name. Updated answer with it. – catch23 Apr 26 '20 at 20:02
1

Try adding the @EntityScan(basePackage="*the package where your entity lies*") (or similar) annotation to your EmployeeDAOImpl class. The query looks alright. For HQL you must use the type name, not the table name. If you're using Spring JPA you can also try to use the provided interfaces like JPARepository or CrudRepository

Sascha R
  • 66
  • 6
1

@Entity should be from JPA library, not hibernate library. And also, you should use the entity name in the query:

session.createQuery("from Employee", Employee.class);