java - 表名未使用 jpa 映射

标签 java spring spring-boot jpa

嗨,我正在使用 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/

相关文章:

java - 尝试在空对象引用上调用虚拟方法 - Fragment Android

java - 为什么我需要在这个 Java 通用子类中有一个构造函数?

spring - 寻找使用另一个组件/注释扩展 Spring MVC 的解决方案

java - Spring 是否以线程安全的方式发布 bean?

spring-boot - 将@SpyBean 与@Qualifier Spring Boot 测试一起使用

java - 比较 JUnit 中的列表。它成功了,但我没有覆盖 equals 。它是如何工作的?

java - 我在启动 eclipse 时收到此错误。我已经重新安装了很多次,仍然收到此错误。?

javascript - jQuery Ajax 请求错误 404(未找到)但它有效

spring-boot - 使用没有 jpa 的 spring-data-rest 但只有 JdbcTemplate

java - Spring Boot Mixpanel : How to set properties to User profile?