java - 无法在教练类(class)上设置教练 car_id

标签 java spring hibernate annotations

我有几个实体,我可以保存讲师的 ID、姓名、姓氏、城市、地址,但我可以使用 thymeleaf 形式在 car_id 上设置他的引用。在引导加载程序上,这很好

模板解析时发生错误(模板:“类路径资源[templates/index.html]”) org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源[templates/index.html]”) 引起原因:org.attoparser.ParseException:计算 SpringEL 表达式时出现异常:“car.getInstructor().id”(模板:“index” - 第 84 行,第 13 列)

在教练课上我这样做了
@OneToOne(级联= CascadeType.ALL,获取= FetchType.LAZY) 私家车; 在汽车课上我这样做了 @一对一 私有(private)教练教练;

<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)类 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;


} 

最佳答案

我正在用这个加载数据

@Component
public class Bootstrap implements CommandLineRunner {

    private final CarService carService;
    private final StudentService studentService;
    private final InstructorService instructorService;
    private final CarDrivingClassService carDrivingClassService;

    public Bootstrap(CarService carService, StudentService studentService, InstructorService instructorService, CarDrivingClassService carDrivingClassService) {
        this.carService = carService;
        this.studentService = studentService;
        this.instructorService = instructorService;
        this.carDrivingClassService = carDrivingClassService;
    }

    @Override
    public void run(String... args) throws Exception {

        Date date = new SimpleDateFormat( "yyyyMMdd" ).parse( "19970807" );
        Date date2 = new SimpleDateFormat( "yyyyMMdd" ).parse( "20192304" );
        Date date3 = new SimpleDateFormat( "yyyyMMdd" ).parse( "20192404" );
        Date date4 = new SimpleDateFormat( "yyyyMMdd" ).parse( "20192505" );

        /*Creating a student*/
        Student student = new Student();
        student.setFirstName("Bob");
        student.setLastName("Rock");
        student.setBirthDate(date);
        student.setCity("Miami");
        student.setAddress("Lincoln Road");
        student.setPostalCode(45300);

        Student student2 = new Student();
        student2.setFirstName("John");
        student2.setLastName("Smith");
        student2.setBirthDate(date);
        student2.setCity("Miami");
        student2.setAddress("Old Cutler Road and Ingraham Highway");
        student2.setPostalCode(45100);

        /*Class that will save our classes for driving*/
        CarDrivingClass savedDrivingClasses;

        //Create 20 car driving classes and save them in table CLASSES
        for(int i = 0; i<20; i++){
            CarDrivingClass tempClass = new CarDrivingClass();
            tempClass.setDate(date3);
            savedDrivingClasses = carDrivingClassService.save(tempClass);
            /*
            Fill table STUDENT_CLASSES
            * */
            student.getClasses().add(savedDrivingClasses);
            student2.getClasses().add(savedDrivingClasses);
            System.out.println("Saving "+(i+1)+ ". class");
        }
        /*Instructor one*/
        Instructor instructor = new Instructor();
        instructor.setFirstName("Dave");
        instructor.setLastName("Joe");
        instructor.setCity("Miami");
        instructor.setAddress("Miami 001");
        instructor.setPostalCode(45400);

        /*Instructor two*/
        Instructor instructor2 = new Instructor();
        instructor2.setFirstName("Clark");
        instructor2.setLastName("Kent");
        instructor2.setCity("LA");
        instructor2.setAddress("South Martel Avenue");
        instructor2.setPostalCode(95400);

        /*Car num one*/
        Car audi = new Car();
        audi.setName("Audi A7");
        audi.setInstructor(instructor);

        /*Car num two*/
        Car BMW = new Car();
        BMW.setName("BMW M4");
        BMW.setInstructor(instructor2);

        /*Adding car to instructors*/
        instructor.setCar(audi);
        instructor2.setCar(BMW);

        /*Associate student with instructor*/
        student.setInstructor(instructor);
        student2.setInstructor(instructor);

        //Saving into db with services
        instructorService.save(instructor);
        instructorService.save(instructor2);
        studentService.save(student);
        studentService.save(student2);
        carService.save(audi);
        carService.save(BMW);

    }
} 

数据显示正常

<h1>Instructors</h1>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Surname</th>
        <th>City</th>
        <th>Address</th>
        <th>Car</th>
    </tr>
    </thead>
    <tr th:remove="all">
        <td>123</td>
        <td>Tasty Goodnees 1</td>
        <td><a href="#">View</a></td>
    </tr>
    <tr th:each="instructor : ${instructors}">
        <td th:text="${instructor.id}">334</td>
        <td th:text="${instructor.firstName}">Tasty Goodnees 3</td>
        <td th:text="${instructor.lastName}">Tasty Goodnees 3</td>
        <td th:text="${instructor.city}">Tasty Goodnees 3</td>
        <td th:text="${instructor.address}">Tasty Goodnees 3</td>
        <td th:text="${instructor.getCar().id}">Tasty Goodnees 3</td> <!--getting car id -->
    </tr>
</table> 

但是当我添加一个新的教练时,它在索引表单中向我显示 ${instructor.getCar().id 的错误,我可以添加教练,而无需表单中 id 的输入字段以及 index.html 中的这行代码 美味的古德尼斯 3

关于java - 无法在教练类(class)上设置教练 car_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050100/

相关文章:

java - 读取在线存储的文本文件

java - 如何在流外访问 Spring Webflow FlowScope 元素?

java - Hibernate Validator 可以用作 Hibernate 之外的组件吗?

java - JPA - 从一列中获取不同的值(value)

Java - 如何使用循环点播放 mp3

java - 线程 "main"java.lang.NoSuchMethodError : main 中出现异常

java - Spring应用程序构建失败

spring - 如何使用spring data jpa获取表的列名

java - 如何通过外部查询订购标准(Hibernate)?

java - 使用 Apache CXF 时,org.bouncycaSTLe.asn1.x509.SubjectPublicKeyInfo 出现 NoClassDefFoundError