簡體   English   中英

Hibernate 表和實體映射查詢異常

[英]Hibernate Query Exception with Table and Entity Mapping

我收到如下錯誤:

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

我錯過了一些東西。
我無法確定到底是什么。

HQL應該如下所示:

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

此外,您可以使用標准 API

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

有用的參考資料:

嘗試在EmployeeDAOImpl class 中添加@EntityScan(basePackage="*the package where your entity lies*") (或類似的)注釋。 查詢看起來沒問題。 對於 HQL,您必須使用類型名稱,而不是表名稱。 如果您使用的是 Spring JPA 您也可以嘗試使用提供的接口,如 JPARepository 或 CrudRepository

@Entity 應該來自 JPA 庫,而不是 hibernate 庫。 此外,您應該在查詢中使用實體名稱:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM