2 Replies Latest reply on Apr 30, 2009 4:44 PM by dzakharov.dmitri.zakharov.gmail.com

    Seam newbie: drop down list value does not persist

    dzakharov.dmitri.zakharov.gmail.com

      Seam: 2.1.1.GA / JBoss 5.0.1.GA


      Hello everybody,


      I've generated a prototype project based on existing database with 'seam generate'. My prototype project allows to submit claims. On the Add Claim form I am trying to replace default text input field for Province with the select drop down list. This is similar to Weather input on Add Round form in open18 example project (ch13) of Seam in Action book. Similar to that example I created Province enum as follows:


      public enum Province {
              AB("Alberta"),
              BC("British Columbia"),
              MB("Manitoba"),
              NB("New Brunswick"),
              NF("Newfoundland and Labrador"), 
              NS("Nova Scotia"),
              NT("Northwest Territories"),
              NU("Nunavut"),
              ON("Ontario"),
              PE("Prince Edward Island"),
              QC("Quebec"),
              SK("Saskatchewan"),
              YK("Yukon Territory");            
              
              private final String longName;
              
              Province(String longName) {
                      this.longName = longName;
              }
              
              public String getLongName() {
                      return this.longName;
              }
              
              public String getLabel() {
                      return name() + " - " + this.longName; 
              }
      }





        • 1. Re: Seam newbie: drop down list value does not persist
          dzakharov.dmitri.zakharov.gmail.com

          Sorry, did not finish my question in the previous post. Here it goes.


          Next thing, I've made the following changes in my Claim.java class (similar to Round.java):


          ...
          import com.fedextnc.domain.credits.model.enums.Province;
          ...
          @Entity
          @Table(name = "CLAIM")
          public class Claim implements java.io.Serializable {
          ...
              private Province importerProvince;
          ...
                  @Enumerated(EnumType.STRING)
                  @NotNull
                  public Province getImporterProvince() {
                          return this.importerProvince;
                  }
                  public void setImporterProvince(Province importerProvince) {
                          this.importerProvince = importerProvince;
                  }
          ...


          In my ClaimHome.java (similar to RoundHome.java) I added the following code:


          ...
              @Factory(value = "provinceList", scope = ScopeType.CONVERSATION)
              public Province[] getProvinceList() {
                  return Province.values();
              }
          ...



          In my ClaimEdit.xhtml (similar to RoundEdit.xhtml) I added the following code:


          ...
          <s:decorate id="importerProvinceField" template="layout/edit.xhtml">
              <ui:define name="label">#{messages['claim.importerProvince']}</ui:define>
              <h:selectOneMenu id="importerProvince" value="#{claimHome.instance.importerProvince}" required="true">
                  <s:selectItems var="_importerProvince" value="#{provinceList}"
                                                          label="#{_importerProvince.label}" noSelectionLabel="-- Select --"/>
                  <s:convertEnum/>
              </h:selectOneMenu>               
          </s:decorate>
          ...



          This perfectly generates the select HTML drop down list as expected on Claim Add form. The HTML source on the generated pages looks like this:


          ...<select id="claim:importerProvinceField:importerProvince" name="claim:importerProvinceField:importerProvince" size="1">       <option value="org.jboss.seam.ui.NoSelectionConverter.noSelectionValue" selected="selected">-- Select --</option>
                  <option value="AB">AB - Alberta</option>
                  <option value="BC">BC - British Columbia</option>
                  <option value="MB">MB - Manitoba</option>
                  <option value="NB">NB - New Brunswick</option>
          
                  <option value="NF">NF - Newfoundland and Labrador</option>
                  <option value="NS">NS - Nova Scotia</option>
                  <option value="NT">NT - Northwest Territories</option>
                  <option value="NU">NU - Nunavut</option>
                  <option value="ON">ON - Ontario</option>
                  <option value="PE">PE - Prince Edward Island</option>
          
                  <option value="QC">QC - Quebec</option>
                  <option value="SK">SK - Saskatchewan</option>
                  <option value="YK">YK - Yukon Territory</option>
          </select>...



          But when I fill up and Claim Add form and click Save button I get the following exception:


          Exception during request processing:
          Caused by javax.servlet.ServletException with message: "#{claimHome.persist}: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.fedextnc.domain.credits.model.Claim]" 



          It seams that Hibernate doesn't know how to persist Province enum value. Am I missing something here?


          Any leads and suggestion are appreciated.

          • 2. Re: Seam newbie: drop down list value does not persist
            dzakharov.dmitri.zakharov.gmail.com

            OK, here's what solved my problem. I returned persistence @Column annotation in the Claim.java, which was initially there after 'seam generate', as follows:


            @Column(name = "IMPORTER_PROVINCE", nullable = false, length = 2)
            @Enumerated(EnumType.STRING)
            @NotNull
            public Province getImporterProvince() {
                return this.importerProvince;
            }



            I am not sure how is it handled in open18 (ch13 - Seam in Action Book) for Weather in Round.java, because there is no @Column anotation there:


            @Enumerated(EnumType.STRING)
            @NotNull
            public Weather getWeather() {
                return this.weather;
            }



            And I did not find any other indications in the open18 (ch13) project sample code. There must be some other trick I am not aware yet about.


            Any comments are appreciated.