persistence@glassfish.java.net

Re: NullPointerException

From: Roan Brasil Monteiro <roanbrasil_at_gmail.com>
Date: Thu, 22 Oct 2009 18:36:05 -0200

it's a web application I am trying to write, I created a bean and deploy and
this didn't work yet.


2009/10/22 Michael Bar-Sinai <mich.barsinai_at_gmail.com>

> Yes, the idea is to create an EJB - as Marina said, you need a JEE class to
> get injections.So that's the whole process here: create the interface and
> the bean, deploy, call the bean.
> Please note that if you're just learning JPA and try to implement it in a
> desktop application, this is not the way at all.... you are writing a
> server-side program, right?
>
> --Michael
>
> On 22 Oct, 2009, at 10:27 PM, Roan Brasil Monteiro wrote:
>
> One Question:
>
> Should I create a interface
>
> @Local
> DaoResourceProviderLocal {
> public BaseDAO getBaseDAO();
> }
>
> @Stateless
> public class DaoResourceProvider implements DaoResourceProviderLocal{
>
> @PersisteceUnit(unitName="citespacePU")
> EntityManagerFactory emf;
>
> ...
> public BaseDAO getBaseDao() {
> BaseDAO retVal = new BaseDAO();
> retVal.setEmf( emf );
> return retVal;
> }
>
> }
>
>
> 2009/10/22 Michael Bar-Sinai <mich.barsinai_at_gmail.com>
>
>> You can get the injected object using an EJB, and then manually inject
>> them to the DAO: (This is from the top of my head, no JEE env here):
>>
>> @Local
>> public class DaoResourceProvider implements DaoResourceProviderLocal {
>>
>> @PersisteceUnit(unitName="citespacePU")
>> EntityManagerFactory emf;
>>
>> ...
>> public BaseDAO getBaseDao() {
>> BaseDAO retVal = new BaseDAO();
>> retVal.setEmf( emf );
>> return retVal;
>> }
>>
>> ...
>>
>>
>> --Michael
>>
>>
>>
>> On 22 Oct, 2009, at 10:07 PM, Roan Brasil Monteiro wrote:
>>
>> Chapter 24?
>>
>> 2009/10/22 Roan Brasil Monteiro <roanbrasil_at_gmail.com>
>>
>>> Just a part. I have a JEE aplication and I would like to split the
>>> persistence part from my JPA class. Do you have some idea? I will take a
>>> look on the documentation.
>>>
>>>
>>> 2009/10/22 Marina Vatkina <Marina.Vatkina_at_sun.com>
>>>
>>>> It depends on what you are trying to achieve in which type of an
>>>> application. Did you look at the JPA cjapter in Java EE 5 Tutorial?
>>>>
>>>> Roan Brasil Monteiro wrote:
>>>>
>>>>> How should I resolve it? How should be my class? I am learning and I
>>>>> am trying to put the persistence part in DAO class.
>>>>>
>>>>> 2009/10/22 Marina Vatkina <Marina.Vatkina_at_sun.com <mailto:
>>>>> Marina.Vatkina_at_sun.com>>
>>>>>
>>>>>
>>>>> You can't inject anything in a class that is neither a Main class,
>>>>> nor a JavaEE component.
>>>>>
>>>>> -marina
>>>>>
>>>>>
>>>>> Roan Brasil Monteiro wrote:
>>>>>
>>>>> I have a class BaseDAO as bellow and I am getting
>>>>> NullPointerException on EntityManager em =
>>>>> emf.createEntityManager(); line from BaseDAO class, Can someone
>>>>> help me how to fix it?
>>>>>
>>>>> public class BaseDAO<T> {
>>>>>
>>>>> @PersistenceUnit(unitName = "citespacePU")
>>>>> EntityManagerFactory emf;
>>>>> public List<T> ListCountry(){
>>>>> EntityManager em = emf.createEntityManager();
>>>>> try{
>>>>> Query q = em.createQuery("Select c from Country c ");
>>>>> List<Country> c = q.getResultList();
>>>>> return (List<T>) c;
>>>>> }finally{
>>>>> if (em != null && em.isOpen()) {
>>>>> em.close();
>>>>> }
>>>>> } }
>>>>>
>>>>>
>>>>> }
>>>>>
>>>>> I have my Jersey resource class:
>>>>>
>>>>> @Path("/user/")
>>>>> public class UserResource {
>>>>> @GET
>>>>> @Produces("text/plain")
>>>>> public String registerUser(){
>>>>> BaseDAO<Country> countryDAO = new
>>>>> BaseDAO<Country>();
>>>>> List<Country> c = countryDAO.ListCountry();
>>>>> String y = "";
>>>>> for(Country x: c){
>>>>> y = y + ":" +x.getName();
>>>>> }
>>>>> return "[Country List]:"+y;
>>>>>
>>>>> }
>>>>>
>>>>> -- Atenciosamente,
>>>>>
>>>>> Roan Brasil Monteiro
>>>>> http://roanbrasil.wordpress.com/
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Atenciosamente,
>>>>>
>>>>> Roan Brasil Monteiro
>>>>> http://roanbrasil.wordpress.com/
>>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Atenciosamente,
>>>
>>> Roan Brasil Monteiro
>>> http://roanbrasil.wordpress.com/
>>>
>>
>>
>>
>> --
>> Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>>
>>
>>
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/
>
>
>


-- 
Atenciosamente,
Roan Brasil Monteiro
http://roanbrasil.wordpress.com/