3 Replies Latest reply on May 21, 2009 9:49 AM by jamesjmp

    unable to inject entityManager

      hi all,
              Here I am doing jsf validation.I have written jsf validation class,but in that class i have injected entityManager.But i am not able to execute the query here. can any one help me i am also sending my code.


      package com.infyz.toms.validator;

      import java.io.Serializable;

      import javax.persistence.EntityManager;
      import javax.persistence.NoResultException;

      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.faces.Validator;
      import org.jboss.seam.annotations.Transactional;
      import javax.faces.component.UIComponent;
      import javax.faces.validator.ValidatorException;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.annotations.Logger;
      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      import com.infyz.toms.master.session.CountrySS;
      import com.infyz.toms.master.entity.Country;
      import org.jboss.seam.Component;
      import org.jboss.seam.annotations.intercept.BypassInterceptors;

      @Name("countryUniqueValidation")
      @Validator
      @Transactional
      @BypassInterceptors
      public class CountryCodeValidation implements javax.faces.validator.Validator, Serializable {
           
           
           private static final long serialVersionUID = 1306935387262157385L;

           @In
           EntityManager em;
           
           @Logger
           private Log log;
           
           public void validate(FacesContext facesContext, UIComponent component,Object value) throws ValidatorException {

                String code = (String) value;
                          Integer countryId = (Integer) component.getAttributes().get("countryId");

                   String qry = null;
                   qry = "select countryId from Country where countryCode = :countryCode";
                   Integer ctrId = 0;
                  
                   try{
                        ctrId = (Integer) em.createQuery(qry)
                          .setParameter("countryCode", code).getSingleResult();
                       
                        if (ctrId != null && !ctrId.equals(countryId)) {
                               throw new ValidatorException(new FacesMessage("Name must be unique"));
                          }
                   }
                  catch (NoResultException nre){
                    // that's fine - this name is unique.
                  }
                  catch (Exception e){
                       //log.error("Runtime Error", e.getMessage(), e.getStackTrace());
                  }
           }

           
      }