1 Reply Latest reply on Dec 8, 2011 6:22 AM by guinotphil

    Seam 2 : Component.getInstance("entityManagerFactory") null

    guinotphil Apprentice

      Hello,

       

       

      A few months ago I did the migration of a Seam 2 / Hibernate 3 application from JBoss AS 7 to JBoss AS 7.

       

       

      This is linked to the issue I had before (cf. http://community.jboss.org/thread/171111 ), but it’s a different problem which I only notice now.

       

       

      I won’t describe all the details of what I did, but here is some information.

       

       

      a) My Hibernate 3 persistence objects are all in a jar project which is deployed in the lib/ directory of the EAR. In persistence.xml my unit name is defined by

      <persistence-unit name="entityManagerFactory" transaction-type="JTA">

       

      And I did the following changes (mainly JNDI names):

      <jta-data-source>java:/MyDS</jta-data-source>

      to

      <jta-data-source>java:jboss/datasources/MyDS</jta-data-source>

       

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

      to

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

       

      And removing

      <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>

       

       

      b) My WAR module has a components.xml file in which I made the following changes:

       

      Use of a JPA2 Persistence Provider

      <!-- http://ctpjava.blogspot.com/2010/07/using-seam-2-with-jpa-2.html -->

      <component name="org.jboss.seam.persistence.persistenceProvider" class="mypackage.HibernateJpa2PersistenceProvider"/>

       

       

      Fixing JNDI issues:

      Changing

      <core:init jndi-pattern="My_EAR_Project/#{ejbName}/local" debug="false" />

      to

      <core:init debug="false" />

       

      Add

      <component class="org.jboss.seam.transaction.EjbSynchronizations" jndi-name="java:app/jboss-seam/EjbSynchronizations"/>

      <!-- component class="org.jboss.seam.async.TimerServiceDispatcher" jndi-name="java:app/jboss-seam/TimerServiceDispatcher"/ -->

      As well as @JndiName annotations on EJB beans.

       

       

       

      And for the entity manager

      <persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{entityManagerFactory}" persistence-unit-jndi-name="java:/MyEntityManagerFactory" />

      becomes

      <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:jboss/MyEntityManagerFactory" />

       

       

      As you can see, I removed the entity-manager-factory attribute because I actually had to remove this part of the components.xml:

      <persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="entityManagerFactory" startupDepends="ehCacheManager"/>

       

       

      This is because, in AS 7 the entityManagerFactory is automatically created at start up, and this would try to create another one and fails :

       

      Could not instantiate Seam component: entityManagerFactory

                at org.jboss.seam.Component.newInstance(Component.java:2170) [jboss-seam.jar:]

                at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304) [jboss-seam.jar:]

                at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278) [jboss-seam.jar:]

                at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:143) [jboss-seam.jar:]

                at org.jboss.seam.init.Initialization.init(Initialization.java:744) [jboss-seam.jar:]

                at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36) [jboss-seam.jar:]

                at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.4.Final.jar:]

                at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.4.Final.jar:]

                at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:77) [jboss-as-web-7.1.0.CR1-SNAPSHOT.jar:]

                at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824)

                at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759)

                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_27]

                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_27]

                at java.lang.Thread.run(Thread.java:662) [:1.6.0_27]

      Caused by: javax.persistence.PersistenceException: [PersistenceUnit: entityManagerFactory] Unable to build EntityManagerFactory

                at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-3.6.6.Final.jar:]

                at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890) [hibernate-entitymanager-3.6.6.Final.jar:]

                at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56) [hibernate-entitymanager-3.6.6.Final.jar:]

                at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) [hibernate-jpa-2.0-api-1.0.1.Final.jar:]

                at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [hibernate-jpa-2.0-api-1.0.1.Final.jar:]

                at org.jboss.seam.persistence.EntityManagerFactory.createEntityManagerFactory(EntityManagerFactory.java:81) [jboss-seam.jar:]

                at org.jboss.seam.persistence.EntityManagerFactory.startup(EntityManagerFactory.java:50) [jboss-seam.jar:]

                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_27]

                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_27]

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_27]

                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_27]

                at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) [jboss-seam.jar:]

                at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144) [jboss-seam.jar:]

                at org.jboss.seam.Component.callComponentMethod(Component.java:2283) [jboss-seam.jar:]

                at org.jboss.seam.Component.callCreateMethod(Component.java:2198) [jboss-seam.jar:]

                at org.jboss.seam.Component.newInstance(Component.java:2158) [jboss-seam.jar:]

                ... 13 more

      Caused by: org.hibernate.MappingException: Cannot cache an unknown entity: mymodel.MyObject

                at org.hibernate.cfg.Configuration.applyCacheConcurrencyStrategy(Configuration.java:2203) [hibernate-core-3.6.6.Final.jar:]

                at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341) [hibernate-core-3.6.6.Final.jar:]

                at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724) [hibernate-core-3.6.6.Final.jar:]

                at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:76) [hibernate-entitymanager-3.6.6.Final.jar:]

                at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) [hibernate-entitymanager-3.6.6.Final.jar:]

                ... 28 more

       

      I'm not really sure about the reason why it fails, I think this is because my model and the persistence.xml are in a different jar, but the main thing is that it try to creates a new Entity Manager Factory which is not what I want.

       

       

      With this setting, I can get the EntityManager via Seam:

      Component.getInstance("entityManager"); => mypackage.Jpa2EntityManagerProxy@38f87c0d

       

       

      But - and this is my problem - I can no longer get the EntityManagerFactory :

      Component.getInstance("entityManagerFactory") => null

       

       

      Is there any way to configure Seam so it can get the entityManagerFactory from JNDI instead of try to create a new one ?

       

       

       

       

      Thank you very much for any help !

        • 1. Re: Seam 2 : Component.getInstance("entityManagerFactory") null
          guinotphil Apprentice

          Here is a workaround I have set up, to be able to use Component.getInstance("entityManagerFactory"). I simply created a stateless component.

           

          Please let me know if you thinkn it's any good, or there is a better way to fix my issue.


          Thank you.

           

           

           

          /**

          * Proxy class for EntityManagerFactory to use as a Seam component

          */

          @Name("entityManagerFactory")

          @Scope(ScopeType.STATELESS)

          @BypassInterceptors

          public class EntityManagerFactoryProxy implements EntityManagerFactory

          {

           

           

                    private final EntityManagerFactory emf;

           

                    public EntityManagerFactoryProxy() {

                              try {

                                        this.emf = (EntityManagerFactory) Naming.getInitialContext().lookup("java:/MyEntityManagerFactory");

                              } catch (NamingException e) {

                                        throw new RuntimeException(e);

                              }

                    }

           

                    @Override

                    public EntityManager createEntityManager()

                    {

                              return emf.createEntityManager();

                    }

           

           

                    @Override

                    public EntityManager createEntityManager(@SuppressWarnings("rawtypes") Map paramMap)

                    {

                              return emf.createEntityManager(paramMap);

                    }

           

           

                    @Override

                    public CriteriaBuilder getCriteriaBuilder()

                    {

                              return emf.getCriteriaBuilder();

                    }

           

           

                    @Override

                    public Metamodel getMetamodel()

                    {

                              return emf.getMetamodel();

                    }

           

           

                    @Override

                    public boolean isOpen()

                    {

                              return emf.isOpen();

                    }

           

           

                    @Override

                    public void close()

                    {

                              emf.close();

                    }

           

           

                    @Override

                    public Map<String, Object> getProperties()

                    {

                              return emf.getProperties();

                    }

           

           

                    @Override

                    public Cache getCache()

                    {

                              return emf.getCache();

                    }

           

           

                    @Override

                    public PersistenceUnitUtil getPersistenceUnitUtil()

                    {

                              return emf.getPersistenceUnitUtil();

                    }

           

          }

          1 of 1 people found this helpful