These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Ejb3Configuration => creating em and emf
PostPosted: Mon Oct 15, 2007 10:34 am 
Newbie

Joined: Sun Dec 17, 2006 6:30 pm
Posts: 9
Hibernate version: 3.2.1 (=> JBoss 4.2.1)

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

Code:
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, MappedSuperclas
s.class, Embeddable.class})
            {

                public boolean accept(String javaElementName)
                {
                    return true;
                }
            };

            //    Add all persistent classes to EJB3 config
            JarVisitor j = JarVisitor.getVisitor(Thread.currentThread().getContextClassLoader().getR
esource("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;
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.