首页 > 解决方案 > 表和实体映射的休眠查询异常

问题描述

我收到如下错误:

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];

我创建了如下所示的模态

@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
}

我的道实现:

@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;
    }
}

我错过了一些东西。
我无法确定到底是什么。

标签: javaspringhibernatespring-boothql

解决方案


HQL应该如下所示:

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

此外,您可以使用Criteria API

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

有用的参考资料:


推荐阅读