Java Spring + Thymeleaf. Как передать сложный объект в форму?
Есть два класса Employee и Department. Employee содержит поле department, которое нужно передать в форму. Такое написание не работает th:field="*{department.title}"
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private long id;
@Column
private String name;
@Column
private String surname;
@Column
private int salary;
@ManyToOne
private Department department;
@ManyToMany(cascade = CascadeType.ALL)
@JsonIgnore
@JoinTable(name = "employees_projects", joinColumns = @JoinColumn(name = "employee_id"),
inverseJoinColumns = @JoinColumn(name = "project_id"))
private List<Project> projectList;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private long id;
@Column
private String title;
@Override
public String toString() {
return "Department{" +
"id=" + id +
", title='" + title + '\'' +
'}';
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "department")
@JsonIgnore
private List<Employee> employeeList;
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>New Employee</title>
</head>
<body>
<form th:method="POST" th:action="@{/employees}" th:object="${employee}">
<label for="name">Name: </label>
<input type="text" th:field="*{name}" id="name"/>
<br>
<label for="surname">Surname</label>
<input type="text" th:field="*{surname}" id="surname"/>
<br>
<label for="salary">Salary</label>
<input type="text" th:field="*{salary}" id="salary"/>
<br>
<label for="department">Department</label>
<input type="text" th:field="*{department.title}" id="department"/>
<input type="submit" value="Create">
</form>
</body>
</html>