开发者

Spring MVC - Child entity lost after submit

开发者 https://www.devze.com 2023-03-03 04:35 出处:网络
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'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!!!).

I hope someone can help me. Thank you in advance! Bye, Dolfiz

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

<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

public class NewPracticeBean implements Serializable{

    private PracticeT practiceT;
    private String action;
    private boolean typeSelected;

    public NewPracticeBean(){
        super();
        thi开发者_运维知识库s.practiceT = new PracticeT();
    }

    // getters & setters...
}

PracticeT.java

public class PracticeT implements java.io.Serializable {

    private long idPractice;
    private PracticeConfT practiceConfT;
    private String opzioneDivisa;

    // getters & setters...
}

PracticeConfT.java

public class PracticeConfT implements java.io.Serializable {

    public static final String PRACTICE_NAME = "practiceName";

    private long idPracticeConf;
    private String practiceName;

    // getters & setters...
}

NewPracticeController.java 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...
}


After spending some time with OptionsTag, OptionWriter and SelectValueComparator, I would say, then output of "selected" is based on Object.equals.

So if for any reason (Lazyloading...) the Object PracticeT.practiceConfT and the according Objects of model.put("practiceTypeList", entitiesList) are not the SAME (==) then forms:options will not select them as long as the equals method is not correct implemented.

So I guess you need to implement a correct equals method, even if this did not fix this problem, it is always better to have a correct equals method than a wrong or none.


Correct implemented means that it must pay attention to the fact that is used with Hibernate. (for example use if (Hibernate.getClass(this) != Hibernate.getClass(other)) instead of `if (this.getClass() != other.getClass() )

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号