Seam3 Persistence Module, using CDI to inject entity manager in Java SE environment
marcilio.grisotto Jun 17, 2012 7:51 AMSeam3 Persistence Module, using CDI to inject entity manager in Java SE environment,
My intention is to create an example using CDI in java SE environment and inject an entity manager. This way it would be easier to test any project's DAO layer, and using maven, this layer could be constructed in an isolated single project.
The description of Persistence Module says:
"It may also bring features handled by Java EE containers to your CDI application running on Java SE"
The section "1.4.1 - Using a Seam-managed persistence context with JPA" of document "http://docs.jboss.org/seam/3/persistence/latest/reference/en-US/html/persistence.html" states that
"This will work even in a SE environment where @PersistenceUnit injection is not normally supported. This is because the seam persistence extensions will bootstrap the EntityManagerFactory for you."
So I did configuration indicated in 'http://docs.jboss.org/seam/3/persistence/latest/reference/en-US/html/', sections 1.2, 1.3.1 and 1.4.1. The maven configuration informed in section 1.2 uses maven properties in the version tags but doesn't inform where it come from, I found it come from seam-bom project that is supposed to be added to the plugin management section of project's pom.xml:
... <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.seam</groupId> <artifactId>seam-bom</artifactId> <version>${seam.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ...
First I used seam.version 3.0.0.Final and run into a lot off error messages, then came to my knowledge that version 3.1.0.Final was available. Switching the version brought some problems like seam-solder-* have became solder-* with package was renamed from org.jboss.seam.solder.* to org.jboss.solder.* and there was some conflict of seam-config-xml with solder-*. After this change I still didn't get any success, so I decided to post my project to github and ask for help.
The project in github is:
> git clone https://github.com/grisottom/cdiJpaEmJavaSE.git
and can be imported as 'Existing Maven Project' to eclipse
there are two ways to run:
1) run BootStrapCDI.java as a Java Application
or
2) run maven "verify" goal of maven-failsafe-plugin that bootStrapCDI in pre-integration-test and run the test "EMConsumerTest":
> mvn verify
either way the result is the same, the entity manager does not get injected (error output is ahead)
For those that doesn't want to download the code but that could give some advice here is the code:
pom.xml
... <properties> <junit.version>4.8.1</junit.version> <compiler-plugin.version>2.3.2</compiler-plugin.version> <jar-plugin.version>2.3.2</jar-plugin.version> <maven-failsafe-plugin.version>2.12</maven-failsafe-plugin.version> <exec.plugin.version>1.2</exec.plugin.version> <maven-surefire-plugin.version>2.12</maven-surefire-plugin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <seam.version>3.1.0.Final</seam.version> <weld.version>1.1.8.Final</weld.version> <hibernate-core.version>4.1.3.Final</hibernate-core.version> <hibernate-commons-annotations.version>4.1.3.Final</hibernate-commons-annotations.version> <com.h2database.version>[1.3.166,)</com.h2database.version> </properties> .... <dependencies> <dependency> <groupId>org.jboss.seam.persistence</groupId> <artifactId>seam-persistence</artifactId> </dependency> <dependency> <groupId>org.jboss.weld.se</groupId> <artifactId>weld-se</artifactId> <version>${weld.version}</version> </dependency> <dependency> <groupId>org.jboss.weld.se</groupId> <artifactId>weld-se-core</artifactId> <version>${weld.version}</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.7.Final</version> <scope>runtime</scope> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${com.h2database.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies>
beans.xml
<?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>
seam-beans.xml
<?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>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" 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"> <persistence-unit name="cdiJpaEmJavaSE" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.hbm2ddl.auto" value="auto"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> <property name="hibernate.connection.driver_class" value="org.h2.Driver"/> <property name="hibernate.connection.url" value="jdbc:h2:~/test"/> <property name="hibernate.connection.username" value="sa"/> </properties> </persistence-unit> </persistence>
EMProducer.java
package any; import javax.enterprise.context.ConversationScoped; import javax.enterprise.inject.Default; import javax.enterprise.inject.Produces; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import org.jboss.solder.core.ExtensionManaged; @Default public class EMProducer { @ExtensionManaged @Produces @ConversationScoped @PersistenceUnit (unitName="cdiJpaEmJavaSE") EntityManagerFactory emf; }
EMConsumer.java
package any; import java.io.Serializable; import javax.inject.Inject; import javax.persistence.EntityManager; public class EMConsumer implements Serializable { private static final long serialVersionUID = 1L; @Inject EntityManager em; public EMConsumer() { System.out.println("*** EMConsumer Bean, default contructor, em injected=" + em); } public Boolean doSomething() { System.out.println("*** EMConsumer.doSomething, em injected =" + em); return (em==null?false:true); } }
and finally
BootStrapCDI.java
package any; import org.jboss.weld.environment.se.Weld; import org.jboss.weld.environment.se.WeldContainer; public class BootStrapCDI { public static void main(String[] args) { System.out.println(" +++ BootStrapCDI using Weld().initialize "); WeldContainer weld = new Weld().initialize(); EMConsumer emConsumer = weld.instance().select(EMConsumer.class).get(); emConsumer.doSomething(); } }
Running this application main() bring this to the console:
+++ BootStrapCDI using Weld().initialize 278 [main] INFO org.jboss.weld.Version - WELD-000900 1.1.8 (Final) 913 [main] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. 16/06/2012 21:58:01 org.jboss.solder.logging.Logger info INFO: Solder Config XML provider starting... 16/06/2012 21:58:01 org.jboss.solder.logging.Logger info INFO: Loading XmlDocumentProvider: org.jboss.solder.config.xml.bootstrap.ResourceLoaderXmlDocumentProvider 16/06/2012 21:58:01 org.jboss.solder.logging.Logger info INFO: Reading XML file: file:/home/marcilio/workspace/cdiJpaEmJavaSE/target/classes/META-INF/seam-beans.xml 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Reading XML file: jar:file:/home/marcilio/bin/java/m2/repository/org/jboss/seam/persistence/seam-persistence/3.1.0.Final/seam-persistence-3.1.0.Final.jar!/META-INF/beans.xml 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Reading XML file: file:/home/marcilio/workspace/cdiJpaEmJavaSE/target/classes/META-INF/beans.xml 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Reading XML file: jar:file:/home/marcilio/bin/java/m2/repository/org/jboss/seam/transaction/seam-transaction/3.1.0.Final/seam-transaction-3.1.0.Final.jar!/META-INF/beans.xml 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Adding XML Defined Bean: org.jboss.seam.transaction.SeSynchronizations 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Adding XML Defined Bean: org.jboss.seam.transaction.EntityTransaction 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Solder 3.1.0.Final (build id: 3.1.0.Final) 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Configuring Seam Managed Persistence Context from producer field any.EMProducer.emf with qualifiers [@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()] 2342 [main] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from org.jboss.seam.persistence.util.EJBContextUtils because of underlying class loading error 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.persistence.hibernate.HibernateManagedSessionExtensionImpl from being installed as bean due to @Veto annotation 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.persistence.HibernatePersistenceProvider from being installed as bean due to @Veto annotation 2618 [main] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from org.jboss.seam.transaction.CMTTransaction because of underlying class loading error 2670 [main] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from org.jboss.seam.transaction.DefaultSeamTransaction because of underlying class loading error 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.transaction.UTTransaction from being installed as bean due to @Veto annotation 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing installation of default bean: org.jboss.seam.transaction.SeSynchronizations 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.transaction.SeSynchronizations from being installed as bean due to @Veto annotation 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.transaction.NoTransaction from being installed as bean due to @Veto annotation 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing installation of default bean: org.jboss.seam.transaction.EntityTransaction 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.transaction.EntityTransaction from being installed as bean due to @Veto annotation 16/06/2012 21:58:02 org.jboss.solder.logging.Logger info INFO: Preventing class org.jboss.seam.transaction.HibernateTransaction from being installed as bean due to @Veto annotation 4311 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled 4311 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled 4320 [main] WARN org.jboss.weld.Reflection - WELD-001450 Interceptor method public java.lang.Object org.jboss.solder.exception.control.ExceptionHandledInterceptor.passExceptionsToSolderCatch(javax.interceptor.InvocationContext) throws java.lang.Throwable does not declare that it throws Exception. 16/06/2012 21:58:04 org.jboss.solder.exception.control.extension.CatchExtension registerHandlerMethod INFO: Adding handler Qualifiers: [@javax.enterprise.inject.Any()] TraversalMode: BREADTH_FIRST Handles Type: class java.lang.Throwable Precedence: -100 [method] public org.jboss.seam.transaction.SimpleTransactionExceptionHandler.markTransactionRollback(CaughtException<Throwable>) to known handlers 16/06/2012 21:58:04 org.jboss.solder.logging.Logger info INFO: Preventing install of default bean Managed Bean [class org.jboss.seam.transaction.TransactionManagerSynchronizations] with qualifiers [@Any @Synthetic] *** EMConsumer Bean, default contructor, em injected=null Exception in thread "main" org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:619) at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:71) at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79) at org.jboss.weld.proxies.EntityManager$ManagedPersistenceContext$-860683961$Proxy$_$$_WeldClientProxy.toString(EntityManager$ManagedPersistenceContext$-860683961$Proxy$_$$_WeldClientProxy.java) at java.lang.String.valueOf(String.java:2826) at java.lang.StringBuilder.append(StringBuilder.java:115) at any.EMConsumer.doSomething(EMConsumer.java:20) at any.BootStrapCDI.main(BootStrapCDI.java:14)
What is this INFO telling me?
"INFO: Preventing class org.jboss.seam.persistence.hibernate.HibernateManagedSessionExtensionImpl from being installed as bean due to @Veto annotation"
can anyone help me to set this project strait?
Thanks for your support
Marcilio