0 Replies Latest reply on Oct 15, 2007 10:30 AM by olvin

    Ejb3Configuration => creating em and emf

    olvin

      Hello,

      I don't want to use anymore the persistence.xml file because the use of jar-file doesn't work (with relative path) if I have the entities on another jar-file in the same ear.

      So I'm trying to create my own entityManagerFactory and entityManager to inject them into JNDI.

      So the code is above.

      But I have still this warning and no SQL Query works ! :
      "WARN [AbstractEntityManagerImpl] Calling joinTransaction() on a non JTA EntityManager"

      So, how can I force the transaction-type to JTA ??? Or another solution ?

      Thanks,

      Olv


      public boolean addEMToJNDI(String datasourceName)
       {
       try
       {
       //DataSource ds = (DataSource) JndiContext.lookup(datasourceName);
       Ejb3Configuration ejbconf = new Ejb3Configuration();
       //ejbconf.setDataSource(ds);
       ejbconf.setProperty("hibernate.session_factory_name", "persistence.units:ear=sts-sal.ear,unitName=PU-SQLBase-SAL57094");
       ejbconf.setProperty("hibernate.connection.datasource", datasourceName);
       ejbconf.setProperty("hibernate.connection.driver_class", "jdbc.gupta.sqlbase.SqlbaseDriver");
       ejbconf.setProperty("hibernate.transaction.factory_class", "org.hibernate.ejb.transaction.JoinableCMTTransactionFactory");
       ejbconf.setProperty("hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JBossTransactionManagerLookup");
       ejbconf.setProperty("hibernate.query.jpaql_strict_compliance", "true");
       ejbconf.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
       ejbconf.setProperty("hibernate.dialect", "stesud.hibernate.dialect.SQLBaseDialect");
       ejbconf.setProperty("hibernate.show_sql", "false");
       ejbconf.setProperty("hibernate.hbm2ddl.auto", "none");
       //ejbconf.setProperty("hibernate.ejb.naming_strategy", "com.fcl.greenfield.naming.FCLNamingStrategy");
       // Find all our persistent classes from ".jar"
       JarVisitor.Filter[] filters = new JarVisitor.Filter[2];
       filters[0] = new JarVisitor.PackageFilter(false, null)
       {
      
       public boolean accept(String javaElementName)
       {
       return true;
       }
       };
       filters[1] = new JarVisitor.ClassFilter(false, new Class[]{Entity.class, MappedSuperclass.class, Embeddable.class})
       {
      
       public boolean accept(String javaElementName)
       {
       return true;
       }
       };
      
       // Add all persistent classes to EJB3 config
       JarVisitor j = JarVisitor.getVisitor(Thread.currentThread().getContextClassLoader().getResource("sts-sal-common.jar"), filters);
       @SuppressWarnings(value = "unchecked")
       Set<JarVisitor.Entry>[] entries = (Set<JarVisitor.Entry>[]) j.getMatchingEntries();
       List<String> classes = new ArrayList<String>();
       for (int i = 0; i < entries.length; i++)
       {
       for (JarVisitor.Entry c : entries)
       {
       CalcLogger.getLogger().info("Found entity " + c.getName());
       ejbconf.addAnnotatedClass(Class.forName(c.getName()));
       }
       }
       CalcLogger.getLogger().debug("Building SessionFactory => em");
       EntityManagerFactory emf = ejbconf.buildEntityManagerFactory();
       ManagedEntityManagerFactory memf = new ManagedEntityManagerFactory(emf, "test");
       InjectedEntityManagerFactory iemf = new InjectedEntityManagerFactory(memf);
       EntityManager em = iemf.createEntityManager();
       CalcLogger.getLogger().debug("Adding EntityManagers and EntityManagerFactories to JNDI");
       JndiContext.addToJndi(GlobalConstants.PU_FACTORY_HEAD_JNDI_NAME.concat("/").concat(datasourceName.substring(datasourceName.length() - 8)), iemf);
       JndiContext.addToJndi(GlobalConstants.PU_HEAD_JNDI_NAME.concat("/").concat(datasourceName.substring(datasourceName.length() - 8)), em);
       CalcLogger.getLogger().info("EntityManagers and EntityManagerFactories added to JNDI");
       return true;
       }
       catch (ClassNotFoundException ex)
       {
       CalcLogger.getLogger().log(TraceLevel.ERROR, null, ex);
       return false;
       }
       catch (IOException ex)
       {
       CalcLogger.getLogger().log(TraceLevel.ERROR, null, ex);
       return false;
       }
       catch (Exception ex)
       {
       CalcLogger.getLogger().log(TraceLevel.ERROR, null, ex);
       return false;
       }
       }