私は春のMVCと春のデータを使用して通常のプロジェクトを作成しました.daoレイヤー、サービスレイヤー、コントローラー、ビューレイヤーがあります。このプロジェクトでは、Department, employee,employeeproject
テーブルがあります。部門を削除すると、対応する従業員テーブルの列departmentId
がnullに設定されます.UIから部門を削除すると、データベースに正しく反映されますが、getALlEmployee
ボタンを介して従業員を再度見ると、その時点で部門の以前の値が表示されます値の代わりにUIがデータベースでnullです。サーバーを再起動すると、値が表示されないのはなぜですか?
これは、3つのレイヤーすべてにある私の関数 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>();
これらの 2 つのクラスの部署と従業員の 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);
}