我正在尝试从使用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();
}
}
答案 0 :(得分:0)
仅当您在查询中提到的表名与实体不匹配时,才会出现此问题。 查询中使用的表名不是真实数据库中的表名,它应该是实体类的名称。如果您试图映射到数据库表并且实际的实体类名称不同,则会抛出此错误。另外请注意,查询字符串应具有与实体类名称完全相同的大小写(大写或小写)。