嗨,我正在使用 Spring+JPA 进行一些 CURD 操作。我已经按照流程创建了所有类和实体类。但是当我运行我的项目时,我收到一条错误消息,指出我的员工未映射。
我去了几个解决方案,他们提到查询实体名称应该与您的实体类名称相同。我也这样做了,但仍然遇到相同的错误。以下是我的代码。
Controller
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接口(interface)
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;
}
}
应用程序属性
#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]"
有人可以帮我解决这个问题吗
最佳答案
您忘记了@ResponseBody
注释。
@GetMapping("/employees")
public @ResponseBody List<Employee> getEmployees() {
return dao.getEmployee();
}
另请记住,返回列表并不是一个好的做法。您最好将列表包装到某个类中,以确保将来您必须添加一些字段,例如“描述”或任何其他字段(实际上什么字段并不重要),您将能够做到这一点.
关于java - 表名未使用 jpa 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61024488/