gpt4 book ai didi

java - Spring/Hibernate CRUD 不工作(MySQL)

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:43 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的 Spring/Hibernate/MySQL CRUD,但它不起作用:/我得到了:

HTTP Status 500 - Internal Server Error

Type Exception Report

Message: Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: employees is not mapped [from employees order by last_name]

描述:服务器遇到意外情况 阻止它完成请求。

这是我的代码:

员工.java

package com.employeemanager.entity;

import java.util.Set;

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="employees")
public class Employee {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email")
private String email;

@Column(name="username")
private String username;

@Column(name="password")
private String password;


public Employee(){

}
public Employee(String firstName){

}
public Employee(String firstName,Set<Project> projects){
this.firstName=firstName;
this.projects=projects;
}

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;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


}

员工DAO.java

package com.employeemanager.dao;

import java.util.List;

import com.employeemanager.entity.Employee;

public interface EmployeeDAO {

public List<Employee> getEmployees();

public void saveEmployee(Employee theEmployee);

public Employee getEmployee(int theId);

public void deleteEmployee(int theId);


}

EmployeeDAOImpl.java

package com.employeemanager.dao;

import java.util.List;

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


import com.employeemanager.entity.Employee;

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {


@Autowired
private SessionFactory sessionFactory;


@Override
public List<Employee> getEmployees() {

Session currentSession=sessionFactory.getCurrentSession();
List<Employee> employeeList=
currentSession.createQuery("from employees order by
last_name").getResultList();
return employeeList;
}

@Override
public void saveEmployee(Employee theEmployee) {

Session currentSession=sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(theEmployee);

}


@Override
public Employee getEmployee(int theId) {

Session currentSession=sessionFactory.getCurrentSession();

Employee theEmployee=currentSession.get(Employee.class, theId);

return theEmployee;

}


@Override
public void deleteEmployee(int theId) {

Session currentSession=sessionFactory.getCurrentSession();

Employee employee=((Employee)
currentSession.load(Employee.class,theId));
if(null!=employee){
this.sessionFactory.getCurrentSession().delete(employee);
}
}

}

员工服务.java

package com.employeemanager.service;

import java.util.List;

import com.employeemanager.entity.Employee;

public interface EmployeeService {

public List<Employee> getEmployees();

public void saveEmployee(Employee theEmployee);

public Employee getEmployee(int theId);

public void deleteEmployee(int theId);


}

EmployeeServiceImpl.java

package com.employeemanager.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.employeemanager.entity.Employee;
import com.employeemanager.dao.EmployeeDAO;

@Service
public class EmployeeServiceImpl implements EmployeeService {

@Autowired
private EmployeeDAO employeeDAO;

@Override
@Transactional
public List<Employee> getEmployees() {

return employeeDAO.getEmployees();
}

@Override
@Transactional
public void saveEmployee(Employee theEmployee) {

employeeDAO.saveEmployee(theEmployee);
}

@Override
@Transactional
public Employee getEmployee(int theId) {

return employeeDAO.getEmployee(theId);
}

@Override
@Transactional
public void deleteEmployee(int theId) {

employeeDAO.deleteEmployee(theId);

}

}

员工 Controller .java

package com.employeemanager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.employeemanager.entity.Employee;
import com.employeemanager.entity.Project;
import com.employeemanager.service.EmployeeService;
import com.employeemanager.service.ProjectService;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

@Autowired
private ProjectService projectService;

@GetMapping("/list")
public String listEmployees(Model theModel){


List<Employee> theEmployees=employeeService.getEmployees();

theModel.addAttribute("employees",theEmployees);

return"employees-list";
}

@GetMapping("/addEmployeeForm")
public String addEmployeeForm(Model theModel){

Employee theEmployee=new Employee();
List<Project> theProjects=projectService.getProjects();
theModel.addAttribute("projects",theProjects);

theModel.addAttribute("employee",theEmployee);

return"employee-form";
}

@PostMapping("/saveEmployee")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee)
{

employeeService.saveEmployee(theEmployee);

return "redirect:/employee/list";
}

@GetMapping("/updateEmployeeForm")
public String updateEmployeeForm(@RequestParam("employeeId") int theId,
Model theModel){

Employee theEmployee=employeeService.getEmployee(theId);
theModel.addAttribute("employee",theEmployee);

return"employee-form";
}

@GetMapping("/deleteEmployee")
public String deleteEmployee(@RequestParam("employeeId") int theId){
employeeService.deleteEmployee(theId);
return"redirect:/employee/list";
}

}

你有什么想法可以解决这个问题吗?感谢您的帮助:)

最佳答案

从此更改您的 HQL:

from employees order by last_name

对此:

from Employee e order by e.lastName

在 HQL 中,您应该使用与映射类相同的命名。

关于java - Spring/Hibernate CRUD 不工作(MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44311234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com