如何使用百里香叶绑定子对象?

时间:2019-07-08 19:44:33

标签: spring spring-boot spring-mvc thymeleaf

我的酒店班级:

 @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long hotelId;
    private String hotelName;
    private String hotelDescription;
    private String hotelAmenties;


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id", referencedColumnName = "id")
    private Address address;

地址类别:

@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String roadNumber;
    private String city;
    private String country;

    @OneToOne(mappedBy = "address")
    private Hotel hotel;

表格:

    <div class="input-field col s12">
     <input id="roadNumber" type="text" class="validate"name="address.roadNumber" placeholder="Road number"/>
    <label for="roadNumber">Road Number</label></div>
  <div class="input-field col s12">
  <input id="city" type="text" class="validate" name="address.city" placeholder="city"/>
  <label for="city">city</label></div>
 <div class="input-field col s12">
   <input id="country" type="text" class="validate" name="address.country" placeholder="country"/>
 <label for="country">Country</label></div>

控制器:

@GetMapping("/addHotel")
    public String addHotelPage(Model model){
        Hotel hotel = new Hotel();
        Address address = new Address();
        model.addAttribute("hotel",hotel);
        return "addhotel";
    }

    @PostMapping("/addHotel")
    public String addHotel(Model model, @ModelAttribute("hotel") @Valid Hotel hotel, BindingResult result,
                           HttpServletRequest request){
        if (result.hasErrors()){
            model.addAttribute("error", true);
            return "addhotel";
        }

        Address address = new Address();
        hotel.setAddress(address);

        model.addAttribute("hotel", hotel);
        model.addAttribute("address", address);

        hotelService.save(hotel);
        return "addhotel";
    }

我想在酒店表和地址表中存储酒店信息和地址信息。当我提交正在保存的酒店信息但未保存地址信息时,上面有一些代码。为什么地址信息未保存在数据库中。我的错误在哪里?

0 个答案:

没有答案