2

I am trying to get a list of employees from the database I had used spring boot and hibernate for doing this project. According to me, I had done everything right but I am getting this ugly exception

org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee];

I had nearly tried all the solution which are on internet some guys suggested that POJO name and name used in query must be same and i had done that also.

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

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

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

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

    @Column(name="email")
    private String email;

    public Employee() {

    }

    public Employee(String firstName, String lastName, String email) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }


}


@Repository
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private EntityManager entity;

    @Override
    @Transactional
    public List<Employee> ListAllEmployee() {

        //create Session
        Session session=entity.unwrap(Session.class);

        Query<Employee> employee=session.createQuery("from Employee",Employee.class);
        return employee.getResultList();
    }

}

Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
  • [org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped](https://stackoverflow.com/questions/23018836/org-hibernate-hql-internal-ast-querysyntaxexception-table-is-not-mapped) – Sudhir Ojha May 13 '19 at 05:07
  • are u using myql if that is the case mysql is case sensitive check your table name in database whether its upper or lower case https://stackoverflow.com/questions/6134006/are-table-names-in-mysql-case-sensitive – Robert Ellis May 13 '19 at 05:19
  • Do you use conventional packaging (app in the root of the tree)? If not, try @EntityScan – Lesiak May 13 '19 at 05:46
  • i had used the same name that is my pojo name is Employee and in query also refers the same Employee – Danzen_mstr May 13 '19 at 05:56
  • 1
    thanks for your suggestions i have found my mistake it was an typo mistake in package name – Danzen_mstr May 13 '19 at 06:54
  • @Danzen_mstr : I am facing the same error could you please help me what was the type mistake – Krishna Apr 27 '20 at 10:51
  • @Danzen_mstr : I am using the same code for learning, Please let me know what was the typo change – Krishna Apr 27 '20 at 10:56

1 Answers1

3

This problem occurs only when the table name you have mentioned in the query is not matching with the entity. The table name used in the query is not the one in the real database, it should be the name of the entity class. If you are trying to map to the database table and actual entity class name is different, it would throw this error. Also note that the query string should have the exact case (Uppercase or Lowercase) of the entity class name to work properly.

Jeeva D
  • 304
  • 1
  • 4
  • 15
  • i had used same name my entity name and name in query is same u can check it from above code – Danzen_mstr May 13 '19 at 05:57
  • "from employee" instead of "from Employee" worked. right? Referring this link https://walkingtechie.blogspot.com/2019/06/difference-between-entity-and-table.html – Satish Patro Jul 13 '21 at 13:26