Results 1 to 2 of 2

Thread: NonUniqueObjectException

  1. #1

    Default NonUniqueObjectException

    Hi,

    i have an object named company which have a property named motherCompany.

    In my jsp form i need to show the list of all the company to choose which is the motherCompany of the actual editable company.

    So i use the referenceData to build it :

    Code:
    List listCompany=companyManager.findAll();
    refData.put("listCompany",  listCompany);
    But when i save my object, it return an NonUniqueObjectException.

    It is right i've got one return from the formBackingObject (the editable company) and one from my listCompany.

    I don't find another way ..., any idea ?

    Thanks,

    Fabien

  2. #2

    Default

    Hey Fabian,

    I just figured this out yesterday myself. Your code is correct, but the next step is how you write your jsp and your controller. Here is an example of the jsp code for populating a drop-down with the list. It assumes that your companyManager manages company domain objects that have an id property, and a name property:

    Code:
    <B>Company&#58;</B>
    <spring&#58;bind path="command.company">
      <FONT color="red">
        <B><c&#58;out value="$&#123;status.errorMessage&#125;"/></B>
      </FONT>
      <SELECT name="companyId">
          <c&#58;forEach var="company" items="$&#123;listCompany&#125;">
            <c&#58;if test="$&#123;command.company.id == company.id&#125;">
              <OPTION selected="<c&#58;out value="$&#123;command.company.id&#125;"/>" value="<c&#58;out value="$&#123;company.id&#125;"/>"><c&#58;out value="$&#123;company.name&#125;"/></OPTION>
            </c&#58;if>
            <c&#58;if test="$&#123;command.company.id != company.id&#125;">
              <OPTION value="<c&#58;out value="$&#123;company.id&#125;"/>"><c&#58;out value="$&#123;company.name&#125;"/></OPTION>
            </c&#58;if>
          </c&#58;forEach>
        </SELECT>
    </spring&#58;bind>
    The next thing to do, in order to make sure the selections "stick", is to add the following to your controller. This example assumes you are assigning the Company to an employee, and that your companyManager can get Company objects from an Id:

    Code:
    protected void onBindAndValidate&#40;HttpServletRequest request, Object command, BindException errors&#41; &#123;
    		Employee employee = &#40;Employee&#41; command;
    		long companyId = Long.parseLong&#40;request.getParameter&#40;"companyId"&#41;&#41;;
          Company company = companyManager.getCompany&#40;companyId&#41;;
    
    		employee.setCompany&#40;company&#41;;
    &#125;
    You would need to modify this to your own needs, but the onBindAndValidate will then set your Domain Object's company to the selection from your jsp.
    I hope that helps.

    Take care,
    James

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •