我試圖做一個簡單的Spring /休眠/ MySQL的CRUD,但它不工作:/我:春/ Hibernate的CRUD不工作(MySQL的)
HTTP狀態500 - 內部服務器錯誤
類型例外報告
消息:請求處理失敗;嵌套的異常是 java.lang.IllegalArgumentException異常: org.hibernate.hql.internal.ast.QuerySyntaxException:僱員不 映射[取自last_name的員工爲了]
描述:服務器遇到意外的條件,即 阻止它履行請求。
這裏是我的代碼:
Employee.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;
}
}
EmployeeDAO.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);
}
}
}
EmployeeService.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);
}
}
EmployeeController.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";
}
}
你有任何想法如何解決這個問題呢? 感謝您對所有您的幫助:)
你表示在application.properties正確的數據庫連接文件? –
我認爲問題在於你試圖使用* table *名稱'employees'而不是實體名稱'Employee'。另請注意,Spring Data JPA可以免費爲您自動生成整個DAO代碼。 – chrylis