openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sim085 <>
Subject @PersistenceContext not working
Date Wed, 22 Dec 2010 14:28:19 GMT

Hello to all, I am having problems with @PersistenceContext. It seems that
this is not initializing the EntityManager and therefore this is remaining
null. My code is as follows;

private EntityManager entityManager;
public User findUser(Long id){
	User user = this.entityManager.find(User.class, id);
	return user;

This code is in a Stateless EJB which is deployed as part of an EAR
application. The EAR application also has a WAR file which makes use of the
above code. [NOTE: I am not using the @PersistenceContext from a Servlet. I
am using it from Stateless EJB. The Servlet calls this Stateless EJB. I am
saying this because I read that @PersistenceContext should not be used from

The problem is that after deployment I get a NullPointerException saying
that entityManager has not been initialized. However if I change the code as
follows everything works fine.

private EntityManager entityManager;
public User findUser(Long id) {
	if(this.entityManager == null){			
		EntityManagerFactory factory = 	
				"TestApp", System.getProperties());
		this.entityManager = factory.createEntityManager();	
		System.out.println("EntityManager initialized using
	User user = this.entityManager.find(User.class, id);
	return user;

As can be seen from the above code I am initializing the
EntityManagerFactory using Persistence.createEntityManagerFactory. I also
tried initializing the EntityManagerFactory using @PersistenceUnit
annotation but this does not work either. In other words it seems that JPA
annotations are not working. 
I did a search over the Internet and I read that the problem could be that
the annotations used as sub-modules of other modules (ex: Under the
WEB-INF/lib folder of the WAR file) do not get their annotations processed.
However I tried to counter this by putting my EJB module in the EAR file and
deleteing the module from the WAR file which is also in the EAR file. The
deployment works fine. However with the second code I still get the message
“EntityManager initialized using EntityManagerFactory” which means that the
@PersistenceContext was not processed. 
Does anyone know what I might be doing wrong!? 
My persistence.xml file is as follows. I am using RESOURCE_LOCAL (which mind
you works on Geronimo but not on GlassFish) because of other problems
related with using data sources. 

    <persistence-unit name="TestApp" transaction-type="RESOURCE_LOCAL">
			<property name="openjpa.jdbc.SynchronizeMappings"
			<property name="openjpa.ConnectionURL"
			<property name="openjpa.ConnectionDriverName"
			<property name="openjpa.ConnectionUserName" value="admin"/>
			<property name="openjpa.ConnectionPassword" value="password"/>
			<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>

I do not know if this is a problem with Geronimo or OpenJPA. The reason I
posted here is because I am using OpenJPA as my Provider and therefore I am
assuming that OpenJPA is responsable to process the @PersistenceContext
annotion. I am not sure of this. Sorry if I posted this in the wrong place.
The @Stateless annotions however are being processed correctly.

View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message