【问题标题】:org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee];org.hibernate.hql.internal.ast.QuerySyntaxException:员工未映射[来自员工];
【发布时间】:2019-09-30 00:51:21
【问题描述】:

我正在尝试从我使用 spring boot 和 hibernate 执行此项目的数据库中获取员工列表。 据我说,我做的一切都是正确的,但我得到了这个丑陋的例外

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

我几乎尝试了互联网上的所有解决方案,有些人建议 POJO 名称和查询中使用的名称必须相同,我也这样做了。

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

}

【问题讨论】:

标签: spring hibernate spring-boot


【解决方案1】:

只有当您在查询中提到的表名与实体不匹配时才会出现此问题。 查询中使用的表名不是真实数据库中的表名,应该是实体类的名称。如果您尝试映射到数据库表并且实际实体类名称不同,则会抛出此错误。另请注意,查询字符串应具有实体类名称的确切大小写(大写或小写)才能正常工作。

【讨论】:

  • 我使用了相同的名称,我的实体名称和查询中的名称相同,您可以从上面的代码中检查它
  • “来自员工”而不是“来自员工”工作。正确的?引用此链接walkingtechie.blogspot.com/2019/06/…
猜你喜欢
  • 2016-03-23
  • 2021-02-27
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
相关资源
最近更新 更多