【问题标题】:Hibernate Query Exception with Table and Entity Mapping表和实体映射的休眠查询异常
【发布时间】:2020-08-10 07:21:07
【问题描述】:

我收到如下错误:

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

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

【问题讨论】:

    标签: java spring hibernate spring-boot hql


    【解决方案1】:

    尝试将@EntityScan(basePackage="*the package where your entity lies*")(或类似)注释添加到您的EmployeeDAOImpl 类。查询看起来没问题。对于 HQL,您必须使用类型名称,而不是表名称。 如果你使用 Spring JPA,你也可以尝试使用 JPARepository 或 CrudRepository 等提供的接口

    【讨论】:

      【解决方案2】:

      HQL 应如下所示:

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

      另外,您可以使用 Criteria API

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

      有用的参考资料:

      【讨论】:

      • 我仍然收到错误,org.hibernate.hql.internal.ast.QuerySyntaxException: employee_list is not mapped [from employee_list];嵌套异常是 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 未映射 [来自employee_list];嵌套异常是 java.lang.IllegalArgumentException
      • Criteria APi,似乎已被弃用
      • @Krishna 误解了我想这是一个本地查询。 HQL 不使用表名。用它更新答案。
      【解决方案3】:

      @Entity 应该来自 JPA 库,而不是休眠库。此外,您应该在查询中使用实体名称:

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

      【讨论】:

        猜你喜欢
        • 2016-10-18
        • 2017-01-22
        • 1970-01-01
        • 2012-03-11
        • 1970-01-01
        • 2014-06-20
        • 2016-02-05
        • 1970-01-01
        相关资源
        最近更新 更多