【问题标题】:JAVA Spring Basic CRUD API Implementation throws errorJAVA Spring Basic CRUD API 实现抛出错误
【发布时间】:2019-12-22 09:29:46
【问题描述】:

我正在使用 Spring 在 JAVA 中创建一个基本的 CRUD 应用程序。 我有一个具有以下配置的 mysql 表 数据库名称: “first_crud_api” 表名: tb1_employee 我在 Employee 表中有 5 列,如下所示.......................... ...

The table description

我正在尝试使用 api 一次从表中检索所有数据...

这是一个 maven 项目,我正在使用端口 8080 中的 Tomcat 运行我的 Spring 应用程序。 当我点击“http://localhost:8080/api/employee”时,我收到错误说明

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Dec 22 14:32:57 IST 2019
There was an unexpected error (type=Internal Server Error, status=500).
org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]

请帮助解决问题...我卡住了

应用类:.....

    @SpringBootApplication
    @Configuration
    @EnableWebMvc
    @ComponentScan("com.sring.crud.*")
    @EnableAutoConfiguration
    public static void main(String[] args) {
        SpringApplication.run(SpringCrudApplication.class, args);
    }

    @Bean
    public EmployeeDAO employeeDAO(){
        return new EmployeeDAOImpl();
    }

    @Bean
    public EmployeeService employeeService(){
        return new EmployeeServiceImpl();
    }


}

Repository 接口及其实现如下

package com.sring.crud.DAO;

import java.util.List;

import com.sring.crud.model.Employee;

public interface EmployeeDAO {

    List<Employee> get();

    Employee get(int id);

    void save (Employee employee);

    void delete (int id);

}

存储库方法的实现:

package com.sring.crud.DAO;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sring.crud.model.Employee;

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

    @Autowired
    private EntityManager entitymanager;

    @SuppressWarnings("deprecation")
    @Override
    public List<Employee> get() {
        // TODO Auto-generated method stub
        Session currentSession = entitymanager.unwrap(Session.class);
        Query<Employee> query = currentSession.createQuery("from Employee",Employee.class);
        return query.getResultList();


    }

    @Override
    public Employee get(int id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void save(Employee employee) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(int id) {
        // TODO Auto-generated method stub

    }

}

Entity类如下:

package com.sring.crud.model;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "tb1_employee")
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private Integer id;
    @Column(name="name")
    private String name;
    @Column(name="gender")
    private String gender;
    @Column(name="department")
    private String department;
    @Column(name="dob")
    private Date dob;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", department=" + department + ", dob="
                + dob + "]";
    }
}

服务接口及其实现如下............

package com.sring.crud.service;

import java.util.List;

import com.sring.crud.model.Employee;

public interface EmployeeService {

    List<Employee> get();

    Employee get(int id);

    void save (Employee employee);

    void delete (int id);
}

实施....

package com.sring.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sring.crud.DAO.EmployeeDAO;
import com.sring.crud.model.Employee;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeDAO employeeDAO;

    @Transactional
    @Override
    public List<Employee> get() {
        // TODO Auto-generated method stub

        return employeeDAO.get();
    }
    @Transactional
    @Override
    public Employee get(int id) {
        // TODO Auto-generated method stub
        return null;
    }
    @Transactional
    @Override
    public void save(Employee employee) {
        // TODO Auto-generated method stub

    }
    @Transactional
    @Override
    public void delete(int id) {
        // TODO Auto-generated method stub

    }

}

【问题讨论】:

  • SpringCrudApplication 类位于哪个包?
  • 包名是“com.spring.crud.SpringCRUDApplication”
  • 由于所有必需的bean都位于应用程序类的包中,因此您无需执行组件扫描。你试过在没有这个注释的情况下运行它吗? @ComponentScan("com.sring.crud.*")

标签: java spring hibernate spring-boot jpa


【解决方案1】:

您似乎没有遵循将Application类放在根包中的最佳做法。

因此,您必须添加大量注释才能使您的应用程序正常工作,但您忘记了 EntityScan。

@EntityScan("com.sring.crud.model")

另外我不确定这是否是在复制粘贴问题中的代码时出现的错误,但注释必须在类级别而不是在主要方法上:

@SpringBootApplication
@Configuration
@EnableWebMvc
@ComponentScan("com.sring.crud.*")
@EntityScan("com.sring.crud.model")
@EnableAutoConfiguration
public static void main(String[] args) {
    SpringApplication.run(SpringCrudApplication.class, args);
}

但我强烈建议您遵循最佳实践并将您的 Application 类放入包中 com.sring 然后您只需要:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringCrudApplication.class, args);
    }
}

请阅读有关构建应用程序代码的文档:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-locating-the-main-class

还有你为什么要创建自己的 DAO?为此目的有 Spring Data JPA: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jpa-and-spring-data

【讨论】:

  • 不知道他的应用类在哪里,所以我问他这个问题
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 2018-05-06
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多