Announcement Announcement Module
No announcement yet.
Spring MVC - Child entity lost after submit Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC - Child entity lost after submit

    Hi all!

    First post on this forum, I hope someone can help me.
    I'm going to try to explain my problem as completely and shortly as I can...

    A web application, made on Spring MVC 2.5 + Hibernate + Java 6 (not using annotation!).

    I've got a controller extending SimpleFormController and a jsp page that is its formView and successView.

    This controller should help me to insert into db an entity PracticeT that has connected (many to one) a lookup entity PracticeConfT (think about it as a "typology"). I need to choose that "typology" through a drop-down menu. In my webapp I need to be able to save data inserted and when I want, to submit the request for approval.

    The page has some text fields and that drop-down menu. The bean called as default "command" is NewPracticeBean that has within a reference to an object PracticeT.

    THE PROBLEM IS: I fill the form, I select a typology from the drop-down menu, I submit form and save data on DB but when I come back to the view, every property is there but the drop-down menu it is not: it has all the options allowed but no one selected. Some checks revealed that the entity PracticeConfT is null (but it has been recorded on db correctly and debugging it is still there in the model until the very end of the method onSubmit!!!).

    Here some useful code:
    (I don't think that hibernate config can be the problem, but if you need it, I can post it too)

    <form:form id="newPracticeForm" commandName="command">
    	<input type="hidden" name="action"/>
    	<spring:nestedPath path="practiceT">
    		<table class="table-data-form">
    				<td class="left"><spring:message code="" text="Practice type" /></td>
    					<form:select path="practiceConfT" multiple="false">
    						<form:option value="" label="- seleziona -"/>
    						<form:options items="${practiceTypeList}" itemValue="idPracticeConf" itemLabel="practiceName"/>	
    				<td class="left">
    					<spring:message code="" text="Opzione divisa" />
    					<br/><form:errors cssClass="errors" path="opzioneDivisa" />
    				<td><form:input path="opzioneDivisa" /></td>
    				<td colspan="1">
    					<input type="submit" name="submit" id="submit" value="Save" class="buttonEMS" style="width:100px;" />
    public class NewPracticeBean implements Serializable{
    	private PracticeT practiceT;
    	private String action;
    	private boolean typeSelected;
    	public NewPracticeBean(){
    		this.practiceT = new PracticeT();
    	// getters & setters...
    public class PracticeT implements {
    	private long idPractice;
    	private PracticeConfT practiceConfT;
    	private String opzioneDivisa;
    	// getters & setters...
    public class PracticeConfT implements {
    	public static final String PRACTICE_NAME = "practiceName";
    	private long idPracticeConf;
    	private String practiceName;
    	// getters & setters...
    public class NewPracticeController extends SimpleFormController{
    	protected SmartLogger log = SmartLogger.getLogger(this.getClass());
    	private PracticeSu practiceSu;
    	private ConfigurationSu configurationSu;
    	private HibernateEntityDataBinder practiceConfTBinder;
    	private HibernateEntityDataBinder practiceTBinder;
    	public NewPracticeController() {
    	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    		log.trace("NewPracticeController -- initBinder");
    		super.initBinder(request, binder);
    		binder.registerCustomEditor(PracticeT.class, "practiceT", practiceTBinder);
    		binder.registerCustomEditor(PracticeConfT.class, "practiceT.practiceConfT", practiceConfTBinder);
    	protected Map referenceData(HttpServletRequest request) throws Exception {
    		log.trace("NewPracticeController -- referenceData");
    		Map model = new HashMap();
    		RetrieveAllEntitiesReq req = new RetrieveAllEntitiesReq();
    		RetrieveAllEntitiesResp resp = configurationSu.retrieveAllEntities(req);
    		List entitiesList = resp.getEntitiesList();
    		model.put("practiceTypeList", entitiesList);
    		return model;
    	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    		NewPracticeBean practiceBean = (NewPracticeBean)command;
    		Map model = errors.getModel();
    		CreateNewPracticeReq req = new CreateNewPracticeReq();
    		CreateNewPracticeResp resp = practiceSu.createNewPractice(req);
    		model.put(getCommandName(), practiceBean);
    		return new ModelAndView(getSuccessView(), model);
    	// setters and getters...