3 Replies Latest reply on Aug 18, 2011 10:36 AM by barcha

    JPA hibernate validator and richfaces a4J

    barcha

      Hi,

      I'm trying to combine jpa 2.0 entity, hibernate validator 4.0 (with annotation) and richfaces validation messages.

      My problem : javax ConstraintViolationException is raised instead of displaying error message in <rich:message/> tag. this happen when a left some field empty while this field contain a notEmpty annotation.

       

      I know i can do it more simple with required attribute of the inputText but i think it's more easy and clear to handle this constraint directly from the entities.

       

      Here is part of the serveur log.

       

      Caused by: javax.validation.ConstraintViolationException: validation failed for classes [metier.entity.Vehicule] during persist time for groups [javax.validation.groups.Default, ]

           at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:155) [:3.6.0.Final]

           at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) [:3.6.0.Final]

           at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:160) [:3.6.0.Final]

       

       

      Here is my entity :

       

      @javax.persistence.Entity

      @Table(name="VEHICULE", uniqueConstraints=@UniqueConstraint(columnNames="VEHICULE_IMMATRICULATION"))

      public class Vehicule implements Entity, Serializable{

       

                /**

                 *

                 */

                private static final long serialVersionUID = 1L;

                @Id

                @GeneratedValue(strategy=GenerationType.AUTO)

                private int vehicule_id;

       

                @Column

                @NotEmpty(message="ce champ est obligatoire")

                private String vehicule_immatriculation;

       

                @Column

                @NotEmpty(message="ce champ est obligatoire")

                private String vehicule_marque;

       

      and here is a part of my JSF managedBean :

       

      public class VehiculeManagedBean implements Serializable{

       

                private static final long serialVersionUID = -2421214031111814582L;

                private static final Logger logger = Logger.getLogger(VehiculeManagedBean.class);

       

          private UIDataTable dataTable = new UIDataTable();

          private HtmlInputHidden dataItemId = new HtmlInputHidden();

       

           // handle all of persistency actions (CRUD) with DAO classes

          private IVehiculeService vehiculeService;

       

          private Vehicule vehiculeEntity;

       

      And here is my xhtml part which should display error message when the fields are empty (it's inside a PopupPanel) :

       

                          <h:form>

                                                                  <h:panelGrid id="editDiv" columns="4">

       

                                                                                      <h:outputText value="marque" />

                                                                                      <h:inputText

                                                                                                value="#{vehiculeManagedBean.vehiculeEntity.marque}"

                                                                                                id="marqueId">

                                                                                      </h:inputText>

                                                                                      <h:outputText value="*" />

                                                                                      <rich:message for="marqueId" />

                                                                                      <h:outputText value="immatriculation" />

                                                                                      <h:inputText

                                                                                                value="#{vehiculeManagedBean.vehiculeEntity.immatriculation}"

                                                                                                id="immatriculationId">

                                                                                      </h:inputText>

                                                                                      <h:outputText value="*" />

                                                                                      <rich:message for="immatriculationId" />

       

                                                                            <h:outputText value="description" />

                                                                            <h:inputTextarea

                                                                                      value="#{vehiculeManagedBean.vehiculeEntity.description}" />

                                                                  </h:panelGrid>

                                                                            <h:outputText value="(*) : Champs obligatoires" />

                                                                            <a4j:commandButton value="valider"

                                                                                      action="#{vehiculeManagedBean.updateDataItem}"

                                                                                      oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editDivPop')}.hide();}"

                                                                                      render="dt" id="submitButton" execute="@form" />

                                                                            <a4j:commandButton value="annuler" id="cancelButton" render="dt"

                                                                                      execute="editDivPop"

                                                                                      onclick="#{rich:component('editDivPop')}.hide(); return false;" />

                     </h:form>

       

       

      I'm waiting for your suggestions, i can't find an issue how to validate my fields directly from my jpa entity.

       

      Thank you so match in advance.