3 Replies Latest reply on Nov 7, 2007 10:14 AM by Richard Teviotdale

    Error selecting object problem

    Jaime Martin Apprentice

      hi,
      In a form I´ve a h:selectOneMenu which is filled according to the values of a List.

       <s:decorate id="firmCodeDecoration" template="layout/edit.xhtml">
       <ui:define name="label">#{messages['Firm']}</ui:define>
       <h:selectOneMenu value="#{rstReportHome.instance.firm}" required="true" id="firm">
       <s:selectItems value="#{firmList.resultList}" var="firm" label="#{firm.firmCode}" />
       <s:convertEntity/>
       <a:support event="onchange" reRender="firmCodeDecoration"/>
       </h:selectOneMenu>
       </s:decorate>
      


      Component firmList is seam-gened with just a simple query this way:
       @Override
       public String getEjbql() {
       return "select firm from Firm firm";
       }


      I want in my selectOneMenu to display all the values of table Firm, but I do want an extra value as well.
      Let´s call it "Automatic".
      I´ve added the following to component firmList:

       @Override
       public List<Firm> getResultList() {
       System.out.println("firmList getResultList!!");
       Firm auto = new Firm();
       auto.setFirmCode("AUTOMATIC");
       CfgCurrency cfgCurrency = new CfgCurrency();
       cfgCurrency.setCurrencyCode(60);
       auto.setCfgCurrency(cfgCurrency);
       List<Firm> firmas = new ArrayList<Firm>();
       firmas.add(auto);
       firmas.addAll(super.getResultList());
       System.out.println("firmas.size: "+firmas.size());
       return firmas;
       }
      


      After adding this, selectOneMenu displays AUTOMATIC value as well as the rest retrieved by the query.
      But when I press a button that performs persist() ajax validation displays red message besides with "Error selecting object" only If I choose Automatic option.
      I guess it must be something related with s:convertEntity, but I´m not sure how to tackle it.

      This is firm object:

      @Entity
      @Table(name = "FIRM", catalog = "prisk")
      public class Firm implements java.io.Serializable {
      
       private String firmCode;
       private CfgCurrency cfgCurrency;
       private String firmDescription;
       private Set<RstReport> rstReports = new HashSet<RstReport>(0);
       private Set<Fund> funds = new HashSet<Fund>(0);
      
       public Firm() {
       firmCode = "";
       firmDescription = "";
       }
      


      What is the best way to add an extra value (apart from those provided by the query) to be displayed in a selectOneMenu?
      If overriding getResultList is one of the ways, why I´m having that validation problem?
      thanks in advance!

        • 1. Re: Error selecting object problem
          Richard Teviotdale Newbie

          I believe you are right about the tag having problems. I think it is because the Automatic object you create has not been persisted and won't have an id.

          Here's a suggestion for something you could try:

          <h:selectOneMenu value="#{rstReportHome.specialfirm}" ...>
          <s:selectItems ... noSelectionLabel="AUTOMATIC"/>
          
          public Firm getSpecialFirm()
          {
           return getInstance().getFirm();
          }
          
          public void setSpecialFirm(Firm firm)
          {
           if(firm == null)
           {
           // create a new automatic firm.
           firm = new Firm();
           firm.setStuff("stuff");
           }
           getInstance().setFirm(firm);
          }
          


          I'm guessing that noSelection returns a null here.
          Please post your results. Thanks.


          • 2. Re: Error selecting object problem
            Jaime Martin Apprentice

            thanks for the idea!
            Error selecting object doesn´t appear anymore, but now it´s turn for this one:
            value is required (javax.faces.component.UIInput.REQUIRED)

            This is the code I´ve added: (following your sample)

            In RstReportHome.java

            
            private Firm specialFirm;
            
             public Firm getSpecialFirm() {
             System.out.println("getSpecialFirm!!");
             return getInstance().getFirm();
             }
            
             public void setSpecialFirm(Firm firm) {
             System.out.println("setSpecialFirm!!");
             if(firm == null) {
             System.out.println("setSpecialFirm - asignando AUTOMATIC!!");
             firm = new Firm();
             firm.setFirmCode("AUTOMATIC");
             CfgCurrency cfgCurrency = new CfgCurrency();
             cfgCurrency.setCurrencyCode(60);
             firm.setCfgCurrency(cfgCurrency);
            
             } else {
             System.out.println("setSpecialFirm - firm no null "+firm.getFirmDescription());
             }
             getInstance().setFirm(firm);
             }
            


            In the view:
             <s:decorate id="firmCodeDecoration" template="layout/edit.xhtml">
             <ui:define name="label">#{messages['Firm']}</ui:define>
             <h:selectOneMenu value="#{rstReportHome.specialFirm}" required="true" id="firm">
             <s:selectItems value="#{firmList.resultList}" var="firm" label="#{firm.firmCode}" noSelectionLabel="AUTOMATIC"/>
             <s:convertEntity/>
             <a:support event="onchange" reRender="firmCodeDecoration"/>
             </h:selectOneMenu>
             </s:decorate>
            


            any further idea? I guess the solution must be near ;-)

            • 3. Re: Error selecting object problem
              Richard Teviotdale Newbie

              The seam documentation says

              noSelectionLabel ? specifies the (optional) label to place at the top of list
              (if required="true" is also specified then selecting this value will cause a
              validation error)

              so try
              <h:selectOneMenu required="false" value="#{rstReportHome.specialFirm}" id="firm">


              I'm not sure if JSF will submit a null to setSpecialFirm() when the non-selection is selected, so you may have to adapt the form's action method to check for this situation and create a new automatic entity.

              P.S. You don't need
              private Firm specialFirm;

              inside RstReportHome.java.