6 Replies Latest reply on Feb 17, 2010 3:21 PM by kamrul

    Hibernate Validation setup problem

    joff

      Hi. I'm trying to get the Hibernate Validation annotations working in my app...


      I have model objects with annotations like:


      @Column()
      @Length( min = 5, max = 10 )
      @NotNull
      private String _testField;




      But my form doesn't seem to be triggering those validations, only the JSF one which specifies if the field is required or not...


      form page looks like (there is a <rich:messages> component in the template..) :


      <s:div id="test-field-container">
        <table class="formtable">
          <ui:decorate template="/layout/form-field.xhtml">
            <ui:define name="label">Test Field</ui:define>
            <h:inputText id="test-field-input" value="${booking.testField}" required="true">
              <rich:ajaxValidator reRender="test-field-container" event="onblur"/>
            </h:inputText>
      </ui:decorate>



      or with <ajax:support reRender="test-field-container" event="onblur" ajaxSingle="true" bypassUpdates="true"/> instead of the <rich:ajaxValidator../>



      I have in my persistence.xml:


      <persistence-unit name="AppName">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <jta-data-source>java:/AppNameDatasource</jta-data-source>
           <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:/BookItEntityManagerFactory"/>
           </properties>
      </persistence-unit>



      and components.xml:


      <persistence:managed-persistence-context 
         name="entityManager" auto-create="true"
         persistence-unit-jndi-name="java:/AppNameEntityManagerFactory"/>



      The Datasource is set up via a standard JBoss -ds.xml file. I'm running Jboss 4.2.2 (but could move to 5 if I really really needed to).


      I read somewhere you need a Seam-managed persistence context in order for the validations to work? Is this not set up like the above? I can run the code through a debugger, and I see the validation code isn't even being hit at all :(


      Any help would be very greatly appreciated, thanks :)



        • 1. Re: Hibernate Validation setup problem
          joff

          Oops, found a typo - the EntityManagerFactory actually does have the same name.


          So, anyone got any ideas?

          • 2. Re: Hibernate Validation setup problem
            jharting

            Have you tried enclosing your input fields with s:validate or s:validateAll instead of using rich:ajaxValidator?

            • 3. Re: Hibernate Validation setup problem
              joff

              Ah yeah I didn't include the form-field.xhtml template - all the input fields have <s:validateAll> around them in that.


              • 4. Re: Hibernate Validation setup problem
                joff

                Well, I found the solution..


                It was because I had the validation annotations on the private fields, and those had names beginning with an underscore character (which is mandated by our code-style over here).


                If I put the validation annotations on the getter for the property, it all works fine.


                Sounds like a bug to me :(


                • 5. Re: Hibernate Validation setup problem
                  kamrul
                  Trye with This process:

                  Enity Bean:

                  @Column(name = "EMAIL", length = 100)
                          @Length(max = 100)
                          @Email
                          public String getEmail() {
                                  return this.email;
                          }


                  xhtml page:

                     <h:inputText id="email" value="#{famsPersonalProfile.email}">
                               <s:validate propagation="none" />
                      </h:inputText> 
                      <rich:message for="email"></rich:message>
                  • 6. Re: Hibernate Validation setup problem
                    kamrul
                    <blockquote>
                    _Md. Kamruzzaman kamrul wrote on Feb 17, 2010 15:17:_<br/>

                    Trye with This process:

                    Enity Bean:

                    @Column(name = "EMAIL", length = 100)
                            @Length(max = 100)
                            @Email
                            public String getEmail() {
                                    return this.email;
                            }


                    xhtml page:

                       <h:inputText id="email" value="#{famsPersonalProfile.email}">
                                 <s:validate />
                        </h:inputText> 
                        <rich:message for="email"></rich:message>
                    </blockquote>

                    Click HELP for text formatting instructions. Then edit this text and check the preview.