java - 未使用 jpa 映射的表名
问题描述
嗨,我正在使用 Spring+JPA 进行一些 CURD 操作。我已经按照流程创建了所有的类和实体类。但是当我运行我的项目时,我收到一条错误消息,说我的Employee 未映射。
我去了一些解决方案,他们提到查询实体名称应该与您的实体类名称相同。我也这样做了,但我仍然遇到相同的错误。以下是我的代码。
控制器
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();
}
}
道接口
package com.springboot.dao;
import java.util.List;
import com.springboot.entity.Employee;
public interface EmployeeDao {
public List<Employee> getEmployee();
}
道实现
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]"
有人可以帮我吗
解决方案
尝试更改“来自员工 e”->“来自员工”。
推荐阅读
- docker - docker 在 windows 服务器核心容器上显示 windows 应用程序
- python - 如何使这个子图在 Python 中工作?
- python - Spyder reports invalid alias when running any script
- java - 嵌套异常是 com.ibatis.common.jdbc.exception.NestedSQLException:
- laravel - how to use laravel Scope in a relationship?
- xml - XSLT 文件 - for-each 函数的问题
- angular - 在 Angular 应用程序中使用 Kendo UI 和 NGX Bootstrap
- java - Vowels and Consonants with Regular Expression
- macos - 在 Mac 上使用 Docker 构建 Hyperledger Fabric
- javascript - 在 js 中保存/加载变量