vendredi 11 novembre 2022

Spring Thymeleaf set checkbox to on update value with related table

Im trying to setup an update/edit page, for an Employee entity that has a oneToMany relation with EmployeeProject entity. Then Project also has a oneToMany relation to EmployeeProject. Saving an employee works fine, you enter a name and contract start/end dates then there is a dynamic list of projects where you use a checkbox to choose the project and add employeeProjectMonths. My problem is in update_employee, when I set the th:field to set the checkbox to show which projects are already set to that employee it uses the EmployeeProject id, not the project id in EmployeeProject, which shows incorrect projects. Im not sure how to do it, ive tried th:field="${employee.employeeProjects.project.id}", also without .id. but I'm not sure how to proceed. So how do I set the th:field="${employee.employeeProjects}" so it chooses the project_id column and not the EmployeeProjects id column?

Heres my entities without constructors, getters and setters.

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @Column(name = "employee_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @Column(name = "contracted_from")
    private String contractedFrom;

    @JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @Column(name = "contracted_to")
    private String contractedTo;

    @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
    private Set<EmployeeProject> employeeProjects = new HashSet<>();

@Entity
@Table(name = "projects")
public class Project implements Serializable {
    @Id
    @Column(name = "project_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long projectNumber;

    @Column(nullable = false, length = 45)
    private String name;


    @JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @Column(name = "start_date", nullable = false)
    private String startDate;

    @JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @Column(name = "end_date", nullable = false)
    private String endDate;

@Entity
@Table(name = "employee_projects")
public class EmployeeProject implements Serializable {

    @Id
    @Column(name = "employee_project_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @ManyToOne
    @JoinColumn(name = "project_id")
    private Project project;

    @Column(name = "employee_booked_months")
    private double employeeBookedMonths;

Controller

@GetMapping("/showFormForEmployeeUpdate/{id}")
    public String showFormForUpdate(@PathVariable ( value = "id") long id, Model model) {
        Employee employee = employeeService.getEmployeeById(id);
        List<Project> projects = employeeService.getAllProjects();
        model.addAttribute("employee", employee);
        model.addAttribute("projects", projects);
        return "update_employee";
    }

html form

<form action="#" th:action="@{/ines/saveEmployee}" th:object="${employee}"
            method="POST">
            
            <!-- Add hidden form field to handle update -->
            <input type="hidden" th:field="*{id}" />

            <input type="text" th:field="*{name}"
                   placeholder="Employee Name" class="form-control mb-4 col-4">

            <input type="date" th:field="*{contractedFrom}"
                   placeholder="Contracted From" class="form-control mb-4 col-4">

            <input type="date" th:field="*{contractedTo}"
                   placeholder="Contracted To" class="form-control mb-4 col-4">
            <div th:each="project : ${projects}">
                <div class="form-group blu-margin">
                    <input type="checkbox" th:field="${employee.employeeProjects}" th:name="projectId" th:text="${project.name}" th:value="${project.id}">
                    <input type="text" th:name="employeeProjectMonths" th:value="${employeeProjectMonths}"
                           placeholder="Employee Project Months" class="form-control mb-4 col-4">
                </div>
            </div>
            <button type="submit" class="btn btn-info col-2">Save Employee</button>
        </form>

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire