on 12-14-2011 11:16 AM
Hi,
We are getting below error when we create an instance of the class which is invoking an entity manager to persist objects using JPA. It would be like a stateless bean creating an instance for the class which is invoking an entity manager to persist objects.
Error:
java.lang.RuntimeException: java.lang.NullPointerException: while trying to invoke the method javax.persistence.EntityManager.persist(java.lang.Object) of an object loaded from field com.persistence.service.manager.impl.CommonPersistenceInterfaceManagerImpl.em of an object loaded from local variable 'this'.
Please let us know are we able to create an instance for Persistable class to reuse it anywhere?.
Code Sample:
// Persistance Manager Class
public class CommonPersistenceInterfaceManagerImpl
{
@PersistenceContext(unitName="SAMPLE")
private EntityManager em;
public String save(Persistable pers)
{
em.persist(pers);
return "True";
}
}
// Stateless webservice
@Stateless
public class ManageImplBean implements Manage {
CommonPersistenceInterfaceManagerImpl cmp;
public ManageImplBean()
{
cmp = new CommonPersistenceInterfaceManagerImpl();
cmp.save(obj);
}
}
PS: If we have created an EntityManager instance in the same webservice then it is working fine.
Best Regards,
Karthika
Hi,
in CommonPersistenceInterfaceManagerImpl the EntityManager em is not injected because CommonPersistenceInterfaceManagerImpl is no StateLess session bean but a pure java class that is instantiated in the other bean's class.
For dependency injection of persistence context, CommonPersistenceInterfaceManagerImpl should itself be a SLSB and should not be instanciated in ManageImplBean but referenced via @EJB.
Regards
Rolf
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.