Привет, я работаю над некоторыми операциями CURD, используя Spring + JPA. Я создал весь класс и класс сущности согласно потоку. Но когда я запускаю свой проект, я получаю сообщение о том, что мой Сотрудник не сопоставлен .
Я обратился к нескольким решениям, в которых упоминалось, что имя объекта запроса должно совпадать. согласно вашему имени класса сущности. Я также сделал это, но все еще получаю ту же ошибку. Следующий код - мой.
Контроллер
package com.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.dao.EmployeeDao;
import com.springboot.entity.Employee;
@RestController
@RequestMapping("/api")
public class EmployeeController {
private EmployeeDao dao;
@Autowired
public EmployeeController(EmployeeDao dao) {
this.dao = dao;
}
@GetMapping("/employees")
public List<Employee> getEmployees() {
return dao.getEmployee();
}
}
Интерфейс Dao
package com.springboot.dao;
import java.util.List;
import com.springboot.entity.Employee;
public interface EmployeeDao {
public List<Employee> getEmployee();
}
DaoImpl
package com.springboot.daoImpl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.springboot.dao.EmployeeDao;
import com.springboot.entity.Employee;
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
private EntityManager em;
@Autowired
public EmployeeDaoImpl(EntityManager em) {
this.em = em;
}
@Override
@Transactional
public List<Employee> getEmployee() {
TypedQuery<Employee> query = em.createQuery("from Employee e",Employee.class);
return query.getResultList();
}
}
Класс сущности сотрудника
package com.springboot.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "emp_seq")
@SequenceGenerator(sequenceName = "emp_seq",allocationSize = 1,initialValue = 1, name = "emp_seq")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String 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;
}
}
Application.property
#JDBC Property
spring.datasource.url=jdbc:postgresql://localhost:5432/emp
spring.datasource.username=postgres
spring.datasource.password=root
# Hibernate properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.show-sql=true
#spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
Ошибка
org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee e]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee e]"
Может кто-нибудь, пожалуйста, помогите мне в этом