私はシンプルな春/休止状態/ 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";
}
}
あなたはこの問題を解決する方法任意のアイデアを持っていますか?あなたはHQLを使用している場合 は、すべてのあなたの助け:)
を働いていた場合、あなたがapplication.propertiesで正しいDB接続をファイルに示されている私たちに教えて? –
私は、エンティティ名 'Employee'の代わりに*テーブル*名前' employees'を使用しようとしているという問題があると思います。また、Spring Data JPAはそのDAOコード全体を無料で自動生成できます。 – chrylis