1 Reply Latest reply on Apr 5, 2011 7:45 PM by MIklos Piklos

    Seam-persistence Tomcat 6. Bug or a feature?

    MIklos Piklos Newbie

      I'm trying to get weld and JPA with seam persistance on tomcat.
      I've got some problems. I managed to solve them i a way. I am wondering if this is good.
      Here's the short version:
      I used maven archetype: jboss-jsf-weld-servlet-webapp
      I added dependencies to seam solder, seam config, and seam persistence, along with jboss logging, and hibernate.
      Here is my beans.xml (located in WEB-INF)

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      Here is my seam-beans.xml (located in classes/META-iNF):

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:s="urn:java:ee" xmlns:t="urn:java:org.jboss.seam.transaction"
                <s:modifies />
                <s:modifies />

      Here is my entityManager producer:

      public class EMFProducer {
           EntityManagerFactory producerField;

      Here is my persistence.xml:

      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
           xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
           <persistence-unit name="pu">
                     <!-- Auto detect annotation model classes -->
                     <property name="hibernate.archive.autodetection" value="class" />
                     <!-- Datasource -->
                     <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                     <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa" />
                     <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
                     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                     <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                     <property name="hibernate.connection.username" value="sa" />
                     <property name="hibernate.connection.password" value="" />

      But when i try to inject entityManager into the HelloWorld.java i get

      Caused by: java.lang.RuntimeException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named
              at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.get

      I looked at a file called:
      ManagedPersistenceContextBeanLifecycle.java line 197 its this method:

      protected EntityManagerFactory getEntityManagerFactory()
            if (emf == null)
               Bean<EntityManagerFactory> bean = (Bean) manager.resolve(manager.getBeans(EntityManagerFactory.class, qualifiers));
               if (bean == null)
                  throw new RuntimeException("Could not find EntityManagerFactory bean with qualifiers" + Arrays.toString(qualifiers));
               CreationalContext<EntityManagerFactory> ctx = manager.createCreationalContext(bean);
               emf = (EntityManagerFactory) manager.getReference(bean, EntityManagerFactory.class, ctx); // line 197
            return emf;

      I made a package org.jboss.seam.persistence in my workspace with the class ManagedPersistenceContextBeanLifecycle.java. I copied the whole class from the seam-persistence.jar here except i changed this:

      protected EntityManagerFactory getEntityManagerFactory()
            if (emf == null)
                 emf = Persistence.createEntityManagerFactory("pu");
            return emf;

      And now everything works. I get the injection, i get the transactional management.

      So tell me am I doing something wrong here, and that's why the recommended way doesn't work? Is there any way i can make it work on the recommended way? Finally how can i get No Persistence provider for EntityManager named when i can get  EntityMAnager like this? What's wrong here?

      Thanks in advance.