Reputation: 50
I am trying to make a simple Spring/Hibernate/MySQL CRUD but it is not working :/ I got:
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]
Description: The server encountered an unexpected condition that prevented it from fulfilling the request.
Here's my code:
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";
}
}
Do you have any ideas how to resolve this problem? Thank you for all your help :)
Upvotes: 0
Views: 140
Reputation: 50
I think that there is an error in EmployeeDAOImpl.java. Change "employees" to "Employee" and "last_name" to "lastName" (names should be the same as in the mapped class(Employee.java))
Just change:
@Override
public List<Employee> getEmployees() {
Session currentSession=sessionFactory.getCurrentSession();
List<Employee> employeeList=
currentSession.createQuery("from employees order by
last_name").getResultList();
return employeeList;
}
to:
@Override
public List<Employee> getEmployees() {
Session currentSession=sessionFactory.getCurrentSession();
List<Employee> employeeList=
currentSession.createQuery("from Employee order by
lastName").getResultList();
return employeeList;
}
and tell us if it worked :)
Upvotes: 1
Reputation: 7127
If you are using HQL, then java Entity names should be used instead of real table names:
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
@Override
public List<Employee> getEmployees() {
Session currentSession = sessionFactory.getCurrentSession();
return currentSession.createQuery("from Employee order by last_name").getResultList();
}
}
Upvotes: 1
Reputation: 2610
Change your HQL from this:
from employees order by last_name
To this:
from Employee e order by e.lastName
In HQL you should use namings the same as your mapped class.
Upvotes: 1