Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC - Child entity lost after submit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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)

    newPractice.jsp
    Code:
    <form:form id="newPracticeForm" commandName="command">
    	<input type="hidden" name="action"/>
    	<spring:nestedPath path="practiceT">
    		<table class="table-data-form">
    			<tr>
    				<td class="left"><spring:message code="" text="Practice type" /></td>
    				<td>
    					<form:select path="practiceConfT" multiple="false">
    						<form:option value="" label="- seleziona -"/>
    						<form:options items="${practiceTypeList}" itemValue="idPracticeConf" itemLabel="practiceName"/>	
    					</form:select>
    				</td>
    			</tr>
    			<tr>
    				<td class="left">
    					<spring:message code="" text="Opzione divisa" />
    					<br/><form:errors cssClass="errors" path="opzioneDivisa" />
    				</td>
    				<td><form:input path="opzioneDivisa" /></td>
    			</tr>
    			<tr>
    				<td colspan="1">
    					<input type="submit" name="submit" id="submit" value="Save" class="buttonEMS" style="width:100px;" />
    				</td>
    			</tr>
    		</table>
    	</spring:nestedPath>
    </form:form>
    NewPracticeBean.java
    Code:
    public class NewPracticeBean implements Serializable{
    	
    	private PracticeT practiceT;
    	private String action;
    	private boolean typeSelected;
    	
    	public NewPracticeBean(){
    		super();
    		this.practiceT = new PracticeT();
    	}
    	
    	// getters & setters...
    }
    PracticeT.java
    Code:
    public class PracticeT implements java.io.Serializable {
    
    	private long idPractice;
    	private PracticeConfT practiceConfT;
    	private String opzioneDivisa;
    	
    	// getters & setters...
    }
    PracticeConfT.java
    Code:
    public class PracticeConfT implements java.io.Serializable {
    	
    	public static final String PRACTICE_NAME = "practiceName";
    
    	private long idPracticeConf;
    	private String practiceName;
    	
    	// getters & setters...
    }
    NewPracticeController.java
    Code:
    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() {
    		setCommandClass(NewPracticeBean.class);
    		setCommandName("command");
    	}
    	
    	@Override
    	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);
    	}
    	
    	@Override
    	protected Map referenceData(HttpServletRequest request) throws Exception {
    		log.trace("NewPracticeController -- referenceData");
    		Map model = new HashMap();
    		RetrieveAllEntitiesReq req = new RetrieveAllEntitiesReq();
    		req.setEntity(PracticeConfT.class);
    		req.setOrderProperty(PracticeConfT.PRACTICE_NAME);
    		RetrieveAllEntitiesResp resp = configurationSu.retrieveAllEntities(req);
    		List entitiesList = resp.getEntitiesList();
    		model.put("practiceTypeList", entitiesList);
    		
    		return model;
    	}
    	
    	@Override
    	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();
    		req.setPracticeT(practiceBean.getPracticeT());
    		CreateNewPracticeResp resp = practiceSu.createNewPractice(req);
    		practiceBean.setPracticeT(resp.getPracticeT());
    		
    		model.putAll(referenceData(null));
    		model.put(getCommandName(), practiceBean);
    			
    		return new ModelAndView(getSuccessView(), model);
    	}
    	
    	// setters and getters...
    }
Working...
X