3 Replies Latest reply on Jun 27, 2006 8:15 AM by holgerprause

    Acces EntityManager from outside of ejb beans(Custome JSF Da

    holgerprause Newbie

      Hello,

      i wrote an own jsf validator(which performs a database lookup) a time ago
      and want to reuse it for my current seam application


      The thing is, in my Validator class i need database access and the EntityManager will be null (i think the reason is the ValidatorClass will be instantiated by jsf , out of the seam context)


      JSP

      <f:validator validatorId="nickNameValidator"/>
      


      JAVA

      @Name("NickNameValidator")
      @Interceptors(SeamInterceptor.class)
      public class NickNameValidator implements Validator {
      
       //@PersistenceContext
       //@In(value="dataSource")
       @PersistenceContext
       private EntityManager entityManager;
       //private IDataSource dataSource;
      
      
       public void validate(FacesContext fc, UIComponent uic, Object o) {
       String nickName = (String)o;
      
       System.out.println("entityManager: "+entityManager);
      
      
       ValidatorException validatorException = checkNickName(fc, nickName);
       if (validatorException != null) {
       ((UIInput)uic).setValid(false);
       throw validatorException;
       }
       }
      }
      


      I know i can perform validation via the Hibernate Framework , but id like to reuse my old cold and i also did not found any examples how to do "database based validation" with the annotation syntax.


      Thank u very much,

      Holger


        • 1. Re: Acces EntityManager from outside of ejb beans(Custome JS
          Pete Muir Master

          You can look up the entity manager in JNDI. In persistence.xml ensure that the EntityManager is registered in JNDI:

          <property name="jboss.entity.manager.jndi.name" value="java:/EntityManager" />


          and then look it up in JNDI in the validator.

          Alternatively you can use Component.newInstance("...") to get a Seam Managed PC (but I'm not sure this is supported).



          • 2. Re: Acces EntityManager from outside of ejb beans(Custome JS
            Jye Tucker Newbie

            Just grab the EntityManagerFactory from JNDI.

            Make sure you have a line like this in persistence.xml to specify where the EMF is stored...

            <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactories/bookingDatabase" />
            


            And use this code to get the EMF and create the EntityManager...

            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
            env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            
            Context ctx = new InitialContext(env);
            EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("java:/EntityManagerFactories/bookingDatabase");
            EntityManager em = emf.createEntityManager();
            


            • 3. Re: Acces EntityManager from outside of ejb beans(Custome JS
              holgerprause Newbie

              Hello,

              Thank u very much u both, it works :-)
              You helped me a lot, i must say i am working with seam for now 5 days and until now, i really have fun, but lets see what challenges ill encounter in future regarding seam ^^

              Thank u again,

              Bye,

              Holger