EntityManager problem
sierras May 24, 2006 11:03 AMHi:
I've two classes like this ones....
@Stateless
public abstract class GeneralEntity
{
@PersistenceContext
public EntityManager em;
public String _EntityName;
protected void setEntityName(String str)
{
_EntityName = str;
}
public String getEntityName()
{
return _EntityName;
}
public int size()
{
String strQuery = "";
try
{
Integer intTableSize;
strQuery = "SELECT COUNT(record) FROM " + getEntityName() + " record";
Query query = em.createQuery(strQuery);
intTableSize = (Integer) query.getSingleResult();
return intTableSize;
}
catch (Exception e)
{
System.out.println("Query executada --> " + strQuery);
e.printStackTrace();
}
return 0;
}
public List getAll()
{
try
{
ArrayList list = new ArrayList();
String str = "FROM " + getEntityName();
Query query = em.createQuery( str );
for ( Object o : query.getResultList() )
{
list.add(o);
}
return list;
}
catch ( Exception e )
{
e.printStackTrace();
}
return null;
}
}
public class CountryBean extends GeneralEntity implements CountryDAO
{
/**
*
*/
private static final long serialVersionUID = 7872740458428627761L;
public CountryBean()
{
this.setEntityName("Country");
}
public Country getByUnitedNationsCode(String id) {
try
{
Country country;
//em is null here no matter what I do...
Query query = em.createQuery("FROM " + getEntityName() + " WHERE _CodiUnitedNations = ?1");
query.setParameter(1, id);
country = (Country) query.getSingleResult();
return country;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
the problem is that when in a client I get the a remote interface and I call the method getAll() from the father class everything goes O.K. Just after that I make call to the getByUnitedNationsCode(...) method of the child class and then em (the variable injected with the entity manager) is null.
Should'nt th derived class get the variable filled from the father class????
Later on I tried to also put
@PersistenceContext
public EntityManager em;
in the child class and it still does'nt work.
Any one has any idea why is not working .....
Thanks in advance.