I am having a couple of entities ,i can save Instructor with his id,name,surname,city,address but i can set his reference on car_id with thymeleaf form .On bootstrap loader it is fine
An error happened during template parsing (template: "class path resource [templates/index.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index.html]") Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "car.getInstructor().id" (template: "index" - line 84, col 13)
In Instructor class i did this
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private Car car;
In Car class i did this
@OneToOne
private Instructor instructor;
<body>
<form th:object="${instructor}" th:action="@{/instructor/}" method="post">
<input type="hidden" th:field="*{id}"/>
First name<br>
<input type="text" class="form-control" th:field="*{firstName}"/><br>
Last name <br>
<input type="text" class="form-control" th:field="*{lastName}"/><br>
City <br>
<input type="text" class="form-control" th:field="*{city}"/><br>
Address <br>
<input type="text" class="form-control" th:field="*{address}"/><br>
<input type="number" class="form-control" th:field="*{car.id}"/><br>
<button type="submit">Submit</button>
</form>
</body>
@Slf4j @Controller public class IndexController {
private final InstructorService instructorService;
private final StudentService studentService;
private final CarService carService;
private final CarDrivingClassService carDrivingClassService;
public IndexController(InstructorService instructorService, StudentService studentService, CarService carService, CarDrivingClassService carDrivingClassService) {
this.instructorService = instructorService;
this.studentService = studentService;
this.carService = carService;
this.carDrivingClassService = carDrivingClassService;
}
@RequestMapping({"", "/", "/index"})
public String getIndexPage(Model model) {
System.out.println("Getting Index page");
model.addAttribute("cars",carService.findAll());
model.addAttribute("instructors", instructorService.findAll());
model.addAttribute("students", studentService.findAll());
return "index";
}
@GetMapping("/new")
public String newRecipe(Model model){
model.addAttribute("instructor", new InstructorCommand());
return "/form";
}
@PostMapping("instructor")
@Transactional
public String saveOrUpdate(@ModelAttribute InstructorCommand command){
InstructorCommand savedCommand = instructorService.saveInstructorCommand(command);
return "redirect:/";
}
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="cars")
public class Car extends BaseEntity{
@Column(name = "name")
private String name;
@OneToOne
private Instructor instructor;
}
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name="instructors")
public class Instructor extends Person{
@OneToMany(mappedBy = "instructor")
public Set<Student> students = new HashSet<>();
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private Car car;
}
car.idmight beidinstead ofcar.id- Deadpool