[jboss-user] [EJB 3.0] - EntityManager Null when trying to inject into stateless sess

BmanT do-not-reply at jboss.com
Wed Apr 25 08:41:27 EDT 2007


Hi,

I'm having an issue deploying to JBoss 4.0.5-ejb3. When I try to inject my EntityManager in my code: 

  | @PersistenceContext EntityManager em;
  | 
it is always null. However, if I do:

  |  EntityManagerFactory emf = Persistence.createEntityManagerFactory("myDS");
  | em = emf.createEntityManager();
  | 

This is my setup:

I have a java web service that calls my Stateless Session bean. In my session bean, I try to inject the PersistenceContext, so I can utilize my Entity Beans.

I have a .ear archive that I deploy to the server/default/deploy directory (using JBuilder 2007) - with a .war (that contains my web service) and a .jar that contains my beans (entity and session). 

I have everything annotated according to the documentation. All of the documentation says that it should work. 
ISampleClass is the @Local Interface class.


  | @Stateless (name="SampleClass")
  | public class SampleClass implements ISampleClass
  |  
  |    @PersistenceContext (unitName="myDS")
  |    EntityManager em;
  |  
  |   public Resource findResource() { 
  |       Resource resource = em.find(Resource.class, 1);
  |       return resource;
  |    }
  | }
  | 

I've also tried: @TransactionAttribute (TransactionAttributeType.REQUIRED)  at the class and method level.


I wrote a client that calls the web service (Axis 1.3 client generated). It calls the service fine, but I get a NullPointerException when it tries to call the em.persist() method inside my stateless session bean.

Here is my persistence.xml file:

  | ><persistence-unit name="myDS"> 
  | 		<provider>org.hibernate.ejb.HibernatePersistence</provider> 
  | 		<jta-data-source>java:/myDS</jta-data-source>
  | 		<properties>
  | 			<property name="hibernate.hbm2ddl.auto" value="validate"></property>
  | 			<property name="hibernate.dialect"
  | 				value="org.hibernate.dialect.MySQLInnoDBDialect" />
  | 			<property name="hibernate.session_factory_name"
  | 				value="java:hibernate/HibernateFactory" />
  | 			<property name="transaction.factory_class"
  | 				value="org.hibernate.transaction.JTATransactionFactory" />
  | 			<property name="hibernate.cache.use_second_level_cache"
  | 				value="false" />
  | 			<property name="hibernate.transaction.manager_lookup_class"
  | 				value="org.hibernate.transaction.JBossTransactionManagerLookup" />
  | 		</properties>
  | 	</persistence-unit>
  | 

I've also tried explicity setting the transaction-type to "JTA" at the persistence-unit level, not setting the provider and leaving the properties section empty.

Here is my mysql-ds.xml file:

  | <datasources>
  | 	<local-tx-datasource>
  | 		<jndi-name>myDS</jndi-name>
  | 		<connection-url>jdbc:mysql://localhost:3306/myDB</connection-url>
  | 		<driver-class>com.mysql.jdbc.Driver</driver-class>
  | 		<user-name>myuser</user-name>
  | 		<password>mypass</password>
  | 		<connection-property name="socketFactory">com.mysql.jdbc.NamedPipeSocketFactory</connection-property>
  | 		<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
  | 		<metadata>
  | 			<type-mapping>mySQL</type-mapping>
  | 		</metadata>
  | 	</local-tx-datasource>
  | </datasources
  | 

And looking at the JBoss logs with debug on I see this (which seems to me that it's loading correctly):

anonymous wrote : 
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] starting service jboss.j2ee:service=EJB3,module=MyApp.jar
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.Ejb3Module] Starting jboss.j2ee:service=EJB3,module=MyApp.jar
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.entity.PersistenceUnitDeployment
  | 2007-04-24 09:00:51,656 INFO  [org.jboss.ejb3.JmxKernelAbstraction] installing MBean: persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS with dependencies
  | 2007-04-24 09:00:51,656 INFO  [org.jboss.ejb3.JmxKernelAbstraction] 	jboss.jca:name=pmacDS,service=DataSourceBinding
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] Creating service persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] adding depends in ServiceController.register: [jboss.jca:name=myDS,service=DataSourceBinding]
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] recording that persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS depends on jboss.jca:name=pmacDS,service=DataSourceBinding
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.ServiceDelegateWrapper] Creating persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.ServiceDelegateWrapper] Created persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] Creating dependent components for: persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS dependents are: []
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] starting service persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
  | 2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.ServiceDelegateWrapper] Starting persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
  | 2007-04-24 09:00:51,671 DEBUG [org.jboss.ejb3.entity.PersistenceUnitDeployment] Found persistence.xml file in EJB3 jar
  | 2007-04-24 09:00:51,687 INFO  [org.hibernate.ejb.Version] Hibernate EntityManager 3.2.0.GA
  | 2007-04-24 09:00:51,703 INFO  [org.hibernate.cfg.annotations.Version] Hibernate Annotations 3.2.0.GA
  | 2007-04-24 09:00:51,718 INFO  [org.hibernate.cfg.Environment] Hibernate 3.2.0.ga
  | 2007-04-24 09:00:51,734 INFO  [org.hibernate.cfg.Environment] hibernate.properties not found
  | 2007-04-24 09:00:51,734 INFO  [org.hibernate.cfg.Environment] Bytecode provider name : javassist
  | 2007-04-24 09:00:51,734 INFO  [org.hibernate.cfg.Environment] using JDK 1.4 java.sql.Timestamp handling 
  | 2007-04-24 09:00:51,828 DEBUG [org.hibernate.ejb.Ejb3Configuration] 2007-04-20 09:31:26,390 DEBUG [org.hibernate.ejb.Ejb3Configuration] Processing PersistenceUnitInfo [
  | name: myDS
  | persistence provider classname: org.hibernate.ejb.HibernatePersistence
  | classloader: org.jboss.mx.loading.UnifiedClassLoader3 at 174a144{ url=file:/C:/jboss-4.0.5.GA/server/default/tmp/deploy/tmp30571MyEAR.ear ,addedOrder=47}
  | Temporary classloader: null
  | excludeUnlistedClasses: false
  | JTA datasource: org.jboss.resource.adapter.jdbc.WrapperDataSource at 4eb98b
  | Non JTA datasource: null
  | Transaction type: JTA
  | PU root URL: file:/C:/jboss-4.0.5.GA/server/default/tmp/deploy/tmp30571MyEAR.ear-contents/MyJAR.jar
  | Jar files URLs []
  | Managed classes names [ <list of my classes>]
  | Mapping files names []
  | Properties [
  | hibernate.jacc.ctx.id: MyJAR.jar
  | hibernate.jndi.java.naming.factory.initial: org.jnp.interfaces.NamingContextFactory
  | hibernate.transaction.manager_lookup_class: org.hibernate.transaction.JBossTransactionManagerLookup
  | hibernate.cache.provider_class: org.hibernate.cache.HashtableCacheProvider
  | hibernate.dialect: org.hibernate.dialect.MySQLInnoDBDialect
  | hibernate.bytecode.use_reflection_optimizer: false
  | hibernate.jndi.java.naming.factory.url.pkgs: org.jboss.naming:org.jnp.interfaces
  | hibernate.bytecode.provider: javassist
  | 

Any ideas? 

Thanks,

Bryan

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4040561#4040561

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4040561



More information about the jboss-user mailing list