Inner object id save in Spring 3

别等时光非礼了梦想. 提交于 2019-12-12 02:49:21

问题


I want to save Village object through hibernate where already persisted District id need to be saved. I populated district object in dropdown. I coded similar work in spring 2, but in spring 3 it doesn't work.

here, if I log village.getDistrict() in POST, id is set perfectly as I set to dropdown but other value of district object is null.

@SessionAttributes({"village"})
@Controller
public class VillageController{

    @Autowired(required=true)
    private AddressService addressService;



   @RequestMapping(value="/cp/village.html", method = RequestMethod.GET)
    public String setForm(ModelMap model) {

    Village village = new Village();    
    village.setDistrict(new District());

    model.addAttribute("village", village);
    return "/cp/village";
    }
    @ModelAttribute("districtList")
    public List<District> populateDistrictList() {
    return addressService.getDistrictList();
    }



 @RequestMapping(value="/cp/village.html", method = RequestMethod.POST)
    public String getForm(@ModelAttribute("village") Village village,               
                BindingResult result, 
                SessionStatus status) {

    log.debug("============================="+village.getDistrict());

    addressService.saveVillage(village);
    status.setComplete(); 

    return "redirect:/cp/village.html123";
    }
}

In JSP:

<form:form commandName ="village" action="village.html" >
      <div>
    <label><fmt:message key="location.district"/></label>
    <form:select path="district.id">
      <form:options items="${districtList}" itemValue="id" itemLabel="districtName"/>
    </form:select>
      </div>

      <div>
    <label><fmt:message key="location.village"/></label>
    <form:input path = "villageName" />    
      </div>


      <div>
    <label><fmt:message key="prompt.remarks"/></label>
    <form:textarea path = "remarks" rows="2" cols="50"/>
      </div>


    <div class = "button-area">
      <input type = "submit" value="Save" class="submit-button" />
      <input type = "button" value="Cancel" onclick="window.location='commonComplaintList.html'" class="submit-button" /> 
    </div>

    <br/>
  </form:form>

回答1:


You do not set any value in the District object, so it is empty.

If you expected that the district has the values of objects from List<District> populateDistrictList(), then I have to say: it does not work out of the box.

One way to do it, would be implementing a Converter, that converts a string (the id) to the District object, by loading it from the database.




回答2:


I think you need to implement a Converter for the District entity as well. Since the Converter for your Village entity class is likely not called at all during the POST (at least not for new Villages, since they lack an id). Also, even if it was called the District would still need a Converter in order to fetch the correct District from Hibernate. Otherwise, Spring will just give you a fresh instance of the District with the Id set to the value in the select.

However, if you are only going to save the Village I am unsure whether you would actually need to fetch the whole District object from Hibernate. I think (not 100% sure on this since it was a while since I used Hibernate and Spring) that Hibernate will associate the village you create in your POST with the correct district even though you only have a District object with an Id. You might need to specify some annotation stuff about cascading for it to not overwrite the District objects other properties.
EDIT: Looked it up a bit and I think you should annotate the district property of the Village class with something like:

@Column(name = "district", insertable = false)

That way Hibernate will know that no new districts should be added when storing a new Village.
Again not sure about this last part, but worth a quick try :)



来源:https://stackoverflow.com/questions/8875681/inner-object-id-save-in-spring-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!