You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by chintan4181 <ch...@gmail.com> on 2011/04/08 01:42:34 UTC

@PersistenceContext is null

Hi,

I am trying to access JPA entities using Stateless EJBs, Earlier i have one
Stateless EJB which was injecting PersistenceContext using annotation. It is
working fine. Since Stateless EJB is haveing data access code, I have
introduced DAO mechanism to separate data access from EJB. For that I have
added below classes 

GenericeDAO:
public interface GenericDAO&lt;E,K extends Serializable&gt; {

	void persist(E entity);
    void remove(E entity);
    E findById(K id);

}

ApplicationDAO: defined DAO as Stateless EJB
@Stateless
public class ApplicationDAO&lt;E,K extends Serializable&gt; implements
GenericDAO&lt;E,K&gt; {

	protected Class entityClass;

	@PersistenceContext(name = "MIApplicationJPA", unitName =
"MIApplicationJPA")
	public EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public ApplicationDAO() {
		ParameterizedType genericSuperclass = (ParameterizedType)
getClass().getGenericSuperclass();
		this.entityClass = (Class) genericSuperclass.getActualTypeArguments()[1];
	}

	public void persist(E entity) { 
		entityManager.persist(entity); 
	}

	public void remove(E entity){ 
		entityManager.remove(entity); 
	}

	public E findById(K id) { 
		E entity;
		entity = (E) entityManager.find(entityClass, id);
		return entity;
	}

}

CertDAOImpl:
public class CertDAOImpl extends ApplicationDAO&lt;Cert,Integer&gt;{

	@Override
	public Certificate findByCertNumber(String certId) throws Exception {

		Certificate cert = (Certificate)
entityManager.createNamedQuery("findByCert_Number")
										   .setParameter("Cert_Number",certId)
										   .getSingleResult();
		return cert;
	}
}

However when i access entityManager from CertDAOImpl. entitymanage is coming
as null. Even though it is in same EJB jar. can anybody tell me what could
be the issue?

Thanks
Chintan

--
View this message in context: http://openjpa.208410.n2.nabble.com/PersistenceContext-is-null-tp6251959p6251959.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: @PersistenceContext is null

Posted by Heath Thomann <jp...@gmail.com>.
If after performing Jeremy's suggestions you find there are no errors, I'd
look at the access of the CertDAOImpl class.  How is this class accessed?
Is it via a container breach, or is it treated as a "helper" class accessed
by other classes?  Since the '@Stateless' annotation is on the parent
(ApplicationDAO), and the CertDAOImpl simply extends it, I think that the
container may not necessarily know about the child class (if the
'@Stateless' annotation was on the CertDAOImpl it would know about the
class)?  If this is correct, then I suspect you are not accessing the
CertDAOImpl class via a container breach.  Without the container on the
thread, you are likely to not get an injected EntityManager.  You stated
"entitymanage is coming as null. Even though it is in same EJB jar."  Folks
often expect EJB Container services for various classes just by virtue of
the fact that the class is contained in an EJB jar.  Unless a class is
managed by the EJB Container, and invoked in a way to breach the EJB
Container, you do not always get EJB Container services for classes
contained in an EJB jar.  Hope this helps.

Thanks,

Heath

On Fri, Apr 8, 2011 at 1:46 PM, Jeremy Bauer <te...@gmail.com> wrote:

> Hi Chintan,
>
> Which application server are you using?  Regardless of the server type, I'd
> start by checking the server logs.  They may show some sort of injection
> failure due to a data source or some other configuration problem.
>
> -Jeremy
>
> On Thu, Apr 7, 2011 at 6:42 PM, chintan4181 <ch...@gmail.com> wrote:
>
> > Hi,
> >
> > I am trying to access JPA entities using Stateless EJBs, Earlier i have
> one
> > Stateless EJB which was injecting PersistenceContext using annotation. It
> > is
> > working fine. Since Stateless EJB is haveing data access code, I have
> > introduced DAO mechanism to separate data access from EJB. For that I
> have
> > added below classes
> >
> > GenericeDAO:
> > public interface GenericDAO&lt;E,K extends Serializable&gt; {
> >
> >        void persist(E entity);
> >    void remove(E entity);
> >    E findById(K id);
> >
> > }
> >
> > ApplicationDAO: defined DAO as Stateless EJB
> > @Stateless
> > public class ApplicationDAO&lt;E,K extends Serializable&gt; implements
> > GenericDAO&lt;E,K&gt; {
> >
> >        protected Class entityClass;
> >
> >        @PersistenceContext(name = "MIApplicationJPA", unitName =
> > "MIApplicationJPA")
> >        public EntityManager entityManager;
> >
> >        @SuppressWarnings("unchecked")
> >        public ApplicationDAO() {
> >                ParameterizedType genericSuperclass = (ParameterizedType)
> > getClass().getGenericSuperclass();
> >                this.entityClass = (Class)
> > genericSuperclass.getActualTypeArguments()[1];
> >        }
> >
> >        public void persist(E entity) {
> >                entityManager.persist(entity);
> >        }
> >
> >        public void remove(E entity){
> >                entityManager.remove(entity);
> >        }
> >
> >        public E findById(K id) {
> >                E entity;
> >                entity = (E) entityManager.find(entityClass, id);
> >                return entity;
> >        }
> >
> > }
> >
> > CertDAOImpl:
> > public class CertDAOImpl extends ApplicationDAO&lt;Cert,Integer&gt;{
> >
> >        @Override
> >        public Certificate findByCertNumber(String certId) throws
> Exception
> > {
> >
> >                Certificate cert = (Certificate)
> > entityManager.createNamedQuery("findByCert_Number")
> >
> >       .setParameter("Cert_Number",certId)
> >
> >       .getSingleResult();
> >                return cert;
> >        }
> > }
> >
> > However when i access entityManager from CertDAOImpl. entitymanage is
> > coming
> > as null. Even though it is in same EJB jar. can anybody tell me what
> could
> > be the issue?
> >
> > Thanks
> > Chintan
> >
> > --
> > View this message in context:
> >
> http://openjpa.208410.n2.nabble.com/PersistenceContext-is-null-tp6251959p6251959.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>

Re: @PersistenceContext is null

Posted by Jeremy Bauer <te...@gmail.com>.
Hi Chintan,

Which application server are you using?  Regardless of the server type, I'd
start by checking the server logs.  They may show some sort of injection
failure due to a data source or some other configuration problem.

-Jeremy

On Thu, Apr 7, 2011 at 6:42 PM, chintan4181 <ch...@gmail.com> wrote:

> Hi,
>
> I am trying to access JPA entities using Stateless EJBs, Earlier i have one
> Stateless EJB which was injecting PersistenceContext using annotation. It
> is
> working fine. Since Stateless EJB is haveing data access code, I have
> introduced DAO mechanism to separate data access from EJB. For that I have
> added below classes
>
> GenericeDAO:
> public interface GenericDAO&lt;E,K extends Serializable&gt; {
>
>        void persist(E entity);
>    void remove(E entity);
>    E findById(K id);
>
> }
>
> ApplicationDAO: defined DAO as Stateless EJB
> @Stateless
> public class ApplicationDAO&lt;E,K extends Serializable&gt; implements
> GenericDAO&lt;E,K&gt; {
>
>        protected Class entityClass;
>
>        @PersistenceContext(name = "MIApplicationJPA", unitName =
> "MIApplicationJPA")
>        public EntityManager entityManager;
>
>        @SuppressWarnings("unchecked")
>        public ApplicationDAO() {
>                ParameterizedType genericSuperclass = (ParameterizedType)
> getClass().getGenericSuperclass();
>                this.entityClass = (Class)
> genericSuperclass.getActualTypeArguments()[1];
>        }
>
>        public void persist(E entity) {
>                entityManager.persist(entity);
>        }
>
>        public void remove(E entity){
>                entityManager.remove(entity);
>        }
>
>        public E findById(K id) {
>                E entity;
>                entity = (E) entityManager.find(entityClass, id);
>                return entity;
>        }
>
> }
>
> CertDAOImpl:
> public class CertDAOImpl extends ApplicationDAO&lt;Cert,Integer&gt;{
>
>        @Override
>        public Certificate findByCertNumber(String certId) throws Exception
> {
>
>                Certificate cert = (Certificate)
> entityManager.createNamedQuery("findByCert_Number")
>
>       .setParameter("Cert_Number",certId)
>
>       .getSingleResult();
>                return cert;
>        }
> }
>
> However when i access entityManager from CertDAOImpl. entitymanage is
> coming
> as null. Even though it is in same EJB jar. can anybody tell me what could
> be the issue?
>
> Thanks
> Chintan
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/PersistenceContext-is-null-tp6251959p6251959.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>