【发布时间】:2020-01-22 19:05:18
【问题描述】:
我有几个实体,我可以用他的 id、姓名、姓氏、城市、地址保存 Instructor,但我可以用百里香形式在 car_id 上设置他的引用。在引导加载程序上很好
模板解析时出错(模板:“类路径资源[templates/index.html]”) org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[templates/index.html]”) 引起:org.attoparser.ParseException:评估 SpringEL 表达式的异常:“car.getInstructor().id”(模板:“index” - 第 84 行,第 13 列)
在讲师课上我做了这个
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
私家车;
在汽车课上我做了这个
@OneToOne
私人讲师讲师;
<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 @控制器 公共类 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.id的形式中可能是id而不是car.id -
我试了还是不行,
标签: java spring hibernate annotations