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.

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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:

3 Comments

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
Criteria APi, seems to be deprecated
@Krishna misunderstood I suppose it was a native query. HQL doesn't use table name. Updated answer with it.
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.