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

    JPA hibernate validator and richfaces a4J

    sila lmozite Newbie


      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 :



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

      public class Vehicule implements Entity, Serializable{





                private static final long serialVersionUID = 1L;



                private int vehicule_id;



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

                private String vehicule_immatriculation;



                @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:panelGrid id="editDiv" columns="4">


                                                                                      <h:outputText value="marque" />





                                                                                      <h:outputText value="*" />

                                                                                      <rich:message for="marqueId" />

                                                                                      <h:outputText value="immatriculation" />





                                                                                      <h:outputText value="*" />

                                                                                      <rich:message for="immatriculationId" />


                                                                            <h:outputText value="description" />


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


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

                                                                            <a4j:commandButton value="valider"


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

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

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


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




      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.