在同一控制器方法中使用Multipart时如何处理外键?

时间:2015-08-24 13:35:35

标签: hibernate spring-mvc

我的下拉列表中包含destination_name。在Place表中,我有一个外键:destination_id,它有一个destination_name。在我的控制器中,当我尝试设置destination_id的值时,我得到错误400说:The request sent by the client was syntactically incorrect.

This is entity class: Place.java

package com.tawesomeness.edikshedik.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class Place {
    @Id
    @GeneratedValue

    private int place_id;
    private String place_name;
    private String place_tagline;
    private String the_place_itself;
    private String googlemap_code;
    private String things_to_do;
    private String accomodation;
    private String food_and_drink;
    private String how_to_reach;
    private String getting_around;
    private String what_to_buy;
    private String banking_facilities;
    private String internet_facilities;
    private String medical_facilities;
    private String police_station;
    @Transient
    private String encodedImage;
    private int destination_destination_id;
}

这是我的控制器方法:

@Transactional
    @RequestMapping(value = "/newPlaceAdded", method = RequestMethod.POST)
    public ModelAndView newPlaceAdded(
                                      @RequestParam("place_name") String place_name,
                                      @RequestParam("destination_destination_id")int destination_destination_id,
                                      @RequestParam("place_tagline") String place_tagline,
                                      @RequestParam("googlemap_code") String googlemap_code,
                                      @RequestParam("things_to_do") String things_to_do,
                                      @RequestParam("accomodation") String accomodation,
                                      @RequestParam("food_and_drink") String food_and_drink,
                                      @RequestParam("how_to_reach") String how_to_reach,
                                      @RequestParam("getting_around") String getting_around,
                                      @RequestParam("what_to_buy") String what_to_buy,
                                      @RequestParam("banking_facilities") String banking_facilities,
                                      @RequestParam("internet_facilities") String internet_facilities,
                                      @RequestParam("medical_facilities") String medical_facilities,
                                      @RequestParam("police_station") String police_station,
                                      @RequestParam("head_image") MultipartFile head_image,
                                      @ModelAttribute("placeAdded")Place p,
                                      ModelAndView model)
    {
        Place place = new Place();
        try
        {
            place.setPlace_name(place_name);
            place.setDestination_destination_id(destination_destination_id);
            place.setPlace_tagline(place_tagline);
            place.setGooglemap_code(googlemap_code);
            place.setThings_to_do(things_to_do);
            place.setAccomodation(accomodation);
            place.setFood_and_drink(food_and_drink);
            place.setHow_to_reach(how_to_reach);
            place.setGetting_around(getting_around);
            place.setWhat_to_buy(what_to_buy);
            place.setBanking_facilities(banking_facilities);
            place.setInternet_facilities(internet_facilities);
            place.setMedical_facilities(medical_facilities);
            place.setPolice_station(police_station);
            place.setHead_image(head_image.getBytes());
            System.out.println(place.getHead_image());

            model.setViewName("hello");
        }catch (Exception e)
        {
            model.addObject("message", "Place not added.");
        }

        System.out.println(place.getDestination_destination_id());
        em.persist(place);
        model.addObject("message", "PlaceAdded");
        return model;
    }

这是我添加下拉列表的视图的一部分:

 <div class="form-group">
                                    <label class="col-sm-2 control-label col-lg-3" for="destination_destination_id">Select the destination of the place: </label>
                                    <div class="col-lg-6">
                                        <select class="form-control" id="destination_destination_id" name="destination_destination_id" required>
                                            <c:forEach var="destinationList" items="${destinationList}">
                                                <option value="${destinationList.destination_id}">${destinationList.destination_name}
                                                </option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>

1 个答案:

答案 0 :(得分:0)

您的问题可能不在您的代码中,而是在数据库架构中 在使用应用程序之前,您应该考虑更多地了解数据库。祝你好运。