我被困在问题上,我使用 spring MVC 和 spring-data 制作了一个正常的项目,其中我有 dao 层、服务层、控制器和视图层。在这个项目中,我有Department, employee,employeeproject
桌子。当我删除部门时,对应的员工表列departmentId
设置为空。所以这在从 UI 中删除部门时正确反映在数据库中,但是当我再次通过getALlEmployee
按钮看到员工时,那时部门的先前值仍显示在UI 而不是值在数据库中为空。当我重新启动服务器时,值没有显示为什么会这样?
这是我在所有三层中的函数 getAllEmployee()
在 DAO 层
/**
* Get the all employee detail from the database
*
* @return list of employees
*/
@Override
public List<Employee> getAllEmployees() {
List<Employee> employeeList = employeeRepository.findAll(new Sort(
Direction.ASC, "employeeNumber"));
return employeeList;
}
在服务层
@Override
public List<EmployeeBO> getAllEmployees() {
List<EmployeeBO> employeeBOList = null;
List<Employee> employeeList = employeeDAO.getAllEmployees();
if (employeeList != null && employeeList.size() != 0) {
employeeBOList = new ArrayList<EmployeeBO>();
for (Employee employee : employeeList) {
employeeBOList.add(convertEmployeeEntityToBO(employee));
}
}
return employeeBOList;
并在控制器中
@RequestMapping(value = "/GetAllEmployee", method = RequestMethod.GET)
public ModelAndView getAllEmployee(
@ModelAttribute("employeeBO") EmployeeBO employeeBO) {
ModelAndView modelAndView = new ModelAndView();
List<EmployeeBO> list = employeeService.getAllEmployees();
modelAndView.addObject("employeeBO", employeeBO);
modelAndView.addObject("listEmployeeBO", list);
modelAndView.setViewName("EmployeeList");
return modelAndView;
}
部门班波佐
@Entity
@Table(name = "department")
public class Department extends BaseObject {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "DEPARTMENT_ID")
private String departmentId;
@Column(name = "DEPARTMENT_NAME")
private String departmentName;
@Column(name = "DEPARTMENT_LOCATION")
private String departmentLocation;
@OneToMany
@JoinColumn(name = "DEPARTMENT_ID", insertable = false, updatable = false)
Collection<Employee> employeeList = new ArrayList<Employee>();
员工波佐班
@Entity
@Table(name = "employee")
public class Employee extends BaseObject {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "EMPLOYEE_NUMBER")
private long employeeNumber;
@Column(name = "FIRST_NAME")
private String firstName;
private String title;
@Column(name = "DEPARTMENT_ID")
private String departmentId;
@Column(name = "MOBILE_NUMBER")
private long mobileNumber;
@Column(name = "DATE_OF_BIRTH")
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
Collection<EmployeeProject> employeeProjectList = new ArrayList<EmployeeProject>();
这两个班级部门和员工 pozo 班级有 setter getter
部门仓库接口
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.nousinfo.tutorial.model.Department;
/**
* Repository interface for {@link Department} instances. Provides basic CRUD
* operations due to the extension of {@link JpaRepository}. Includes custom
* implemented functionality by extending {@link JpaRepository}.
*
* @author ankurj
*/
public interface DepartmentRepository extends JpaRepository<Department, String> {
/**
* Find all Department with the given department Name. This method will be
* translated into a query by constructing it directly from the method name
* as there is no other query declared.
*
* @param name
* departmentName to be find
* @return list of departments
*/
public List<Department> findByDepartmentNameLike(String name);
/**
*
* update the employee with the given departmentId. This method will be
* translated into a query using the one declared in the {@link Query}
* annotation declared one.
*
* @param departmentId
* departmentId to be set
* @param deptId
* departmentId is to deleted
* @return integer value
*/
@Modifying
@Query("UPDATE Employee set DEPARTMENT_ID = :departmentId WHERE DEPARTMENT_ID = :deptId")
public int updateEmployeeDepartmentId(
@Param("departmentId") String departmentId,
@Param("deptId") String deptId);
}