Seam-persistence Tomcat 6. Bug or a feature?
piklos Apr 5, 2011 5:04 PMI'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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd"> <interceptors> <class>org.jboss.seam.transaction.TransactionInterceptor</class> </interceptors> </beans>
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" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd"> <t:SeSynchronizations> <s:modifies /> </t:SeSynchronizations> <t:EntityTransaction> <s:modifies /> </t:EntityTransaction> </beans>
Here is my entityManager producer:
public class EMFProducer { @ExtensionManaged @Produces @PersistenceUnit(name="pu") @ConversationScoped EntityManagerFactory producerField; }
Here is my persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="pu"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <!-- 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="" /> </properties> </persistence-unit> </persistence>
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 EntityManagerFactory(ManagedPersistenceContextBeanLifecycle.java:197)
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.