You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Anas Mughal <an...@yahoo.com> on 2010/11/10 06:52:04 UTC

session is NULL

I have setup my Tapestry project using the Maven archetype. Then, I setup my hibernate.cfg.xml file with references to my hibernate mapping files. (I am not using annotations for hibernate.)

Now, I have setup a simple DAO object to try to retrieve an object from the database:

public class BranchDAO {

    @Inject
    private Session session;

    @SuppressWarnings("unchecked")
    public Object find( Class c , BigDecimal id)
    {
        return  session.get(c, id);  // session is NULL here
    }
}


I get a NULL pointer exception because my session does not seem to be
 initialized.

Searching online, I came accross:
http://wiki.apache.org/tapestry/SessionPagePersistence

I have not setup any hivemind configuration or any of the suggested classes on that wiki page. Please advise me what do I need to be able to fetch objects using Tapestry-Hibernate. I don't know where to place the hivemind configuration file -- if I need it.

I am new to Tapestry. Please bear with me.  

Thank you for your kind assistance. 
--Anas Mughal 
http://anas-mughal.com

 


      

Re: session is NULL

Posted by ael <al...@dash.com.ph>.
Kindly visit my post.

http://tapestry.1045711.n5.nabble.com/T5-Working-Tapestry-Hibernate-with-DAO-td3229905.html#a3229905
http://tapestry.1045711.n5.nabble.com/T5-Working-Tapestry-Hibernate-with-DAO-td3229905.html#a3229905 
-- 
View this message in context: http://tapestry.1045711.n5.nabble.com/session-is-NULL-tp3258165p3258344.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: session is NULL

Posted by Josh Kamau <jo...@gmail.com>.
Anas,

Your Dao must be a service managed by tapestry for it to benefit from the
injection of the rest of tapestry services. To ensure that ,
make sure you bind it in the application module. If you used the archetype
to generate the application, then there is  module class in the services
directory. The class is well commented with an example on how to bind a
service

Something like bind(MyDaoInterface.class ,
MyDaoInterfaceImplementation.class);


regards.
On Wed, Nov 10, 2010 at 1:40 AM, Anas Mughal <an...@yahoo.com> wrote:

> Yes, tapestry-hibernate dependency is added:
>
> <dependency>
> <groupId>org.apache.tapestry</groupId>
> <artifactId>tapestry-hibernate</artifactId>
> <version>5.2.2</version>
> </dependency>
>
> And, it is being resolved and included successfully in the project.
>
> The hibernate configuration seems to be picked up properly as well. (There
> was a problem with the path to the mapping files. After fixing it, now there
> is no error with respect to the hibernate configuration.)
>
> I don't know how to resolve the session object being NULL in my simple DAO
> below.
>
>
>
>
>
>
>
> Thanks for your help!
> --
> Anas Mughal
>
> http://anas-mughal.com
>
>
>
>
>
>
> --- On Wed, 11/10/10, Josh Kamau <jo...@gmail.com> wrote:
>
>
> From: Josh Kamau <jo...@gmail.com>
> Subject: Re: session is NULL
> To: "Tapestry users" <us...@tapestry.apache.org>
> Date: Wednesday, November 10, 2010, 1:04 AM
>
>
> Hi Anas;
>
> Have you added the tapestry-hibernate dependency on your pom.xml?
>
> On Wed, Nov 10, 2010 at 12:52 AM, Anas Mughal <an...@yahoo.com>
> wrote:
>
> > I have setup my Tapestry project using the Maven archetype. Then, I setup
> > my hibernate.cfg.xml file with references to my hibernate mapping files.
> (I
> > am not using annotations for hibernate.)
> >
> > Now, I have setup a simple DAO object to try to retrieve an object from
> the
> > database:
> >
> > public class BranchDAO {
> >
> >     @Inject
> >     private Session session;
> >
> >     @SuppressWarnings("unchecked")
> >     public Object find( Class c , BigDecimal id)
> >     {
> >         return  session.get(c, id);  // session is NULL here
> >     }
> > }
> >
> >
> > I get a NULL pointer exception because my session does not seem to be
> >  initialized.
> >
> > Searching online, I came accross:
> > http://wiki.apache.org/tapestry/SessionPagePersistence
> >
> > I have not setup any hivemind configuration or any of the suggested
> classes
> > on that wiki page. Please advise me what do I need to be able to fetch
> > objects using Tapestry-Hibernate. I don't know where to place the
> hivemind
> > configuration file -- if I need it.
> >
> > I am new to Tapestry. Please bear with me.
> >
> > Thank you for your kind assistance.
> > --Anas Mughal
> > http://anas-mughal.com
> >
> >
> >
> >
> >
>
>
>
>
>

Re: session is NULL

Posted by Anas Mughal <an...@yahoo.com>.
Yes, tapestry-hibernate dependency is added:
 
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>5.2.2</version>
</dependency>
 
And, it is being resolved and included successfully in the project.
 
The hibernate configuration seems to be picked up properly as well. (There was a problem with the path to the mapping files. After fixing it, now there is no error with respect to the hibernate configuration.)

I don't know how to resolve the session object being NULL in my simple DAO below.







Thanks for your help!
--
Anas Mughal

http://anas-mughal.com 
 
 
 
 
 

--- On Wed, 11/10/10, Josh Kamau <jo...@gmail.com> wrote:


From: Josh Kamau <jo...@gmail.com>
Subject: Re: session is NULL
To: "Tapestry users" <us...@tapestry.apache.org>
Date: Wednesday, November 10, 2010, 1:04 AM


Hi Anas;

Have you added the tapestry-hibernate dependency on your pom.xml?

On Wed, Nov 10, 2010 at 12:52 AM, Anas Mughal <an...@yahoo.com> wrote:

> I have setup my Tapestry project using the Maven archetype. Then, I setup
> my hibernate.cfg.xml file with references to my hibernate mapping files. (I
> am not using annotations for hibernate.)
>
> Now, I have setup a simple DAO object to try to retrieve an object from the
> database:
>
> public class BranchDAO {
>
>     @Inject
>     private Session session;
>
>     @SuppressWarnings("unchecked")
>     public Object find( Class c , BigDecimal id)
>     {
>         return  session.get(c, id);  // session is NULL here
>     }
> }
>
>
> I get a NULL pointer exception because my session does not seem to be
>  initialized.
>
> Searching online, I came accross:
> http://wiki.apache.org/tapestry/SessionPagePersistence
>
> I have not setup any hivemind configuration or any of the suggested classes
> on that wiki page. Please advise me what do I need to be able to fetch
> objects using Tapestry-Hibernate. I don't know where to place the hivemind
> configuration file -- if I need it.
>
> I am new to Tapestry. Please bear with me.
>
> Thank you for your kind assistance.
> --Anas Mughal
> http://anas-mughal.com
>
>
>
>
>



      

Re: session is NULL

Posted by Josh Kamau <jo...@gmail.com>.
Hi Anas;

Have you added the tapestry-hibernate dependency on your pom.xml?

On Wed, Nov 10, 2010 at 12:52 AM, Anas Mughal <an...@yahoo.com> wrote:

> I have setup my Tapestry project using the Maven archetype. Then, I setup
> my hibernate.cfg.xml file with references to my hibernate mapping files. (I
> am not using annotations for hibernate.)
>
> Now, I have setup a simple DAO object to try to retrieve an object from the
> database:
>
> public class BranchDAO {
>
>     @Inject
>     private Session session;
>
>     @SuppressWarnings("unchecked")
>     public Object find( Class c , BigDecimal id)
>     {
>         return  session.get(c, id);  // session is NULL here
>     }
> }
>
>
> I get a NULL pointer exception because my session does not seem to be
>  initialized.
>
> Searching online, I came accross:
> http://wiki.apache.org/tapestry/SessionPagePersistence
>
> I have not setup any hivemind configuration or any of the suggested classes
> on that wiki page. Please advise me what do I need to be able to fetch
> objects using Tapestry-Hibernate. I don't know where to place the hivemind
> configuration file -- if I need it.
>
> I am new to Tapestry. Please bear with me.
>
> Thank you for your kind assistance.
> --Anas Mughal
> http://anas-mughal.com
>
>
>
>
>

Re: session is NULL

Posted by Cezary Biernacki <ce...@gmail.com>.
On Wed, Nov 10, 2010 at 9:42 AM, Anas Mughal <an...@yahoo.com> wrote:

>
> I have setup my DAO as a Tapestry service in the AppModule as:
>
>
[...]


Then, I try to inject the session as follows:
>
>   public class BranchHibernateDAO implements BranchDAO {
>
>      @Inject
>      private Session session;
>
>
>      @SuppressWarnings("unchecked")
>      public Object find( Class c , BigDecimal id)
>      {
>        return session.get(c, id);
>      }
>
>   }
>
>
> I still get a NULL session.
>
>
> Following the suggestion by Kalle, I injected the session to a page. Then,
> passed the session to my DAO in the contructor. That worked! However, I
> would rather keep the code cleaner by injecting the session directly into
> the DAO.
>
> Any suggestion would be greatly appreciated.
>


How do you access an instance of BranchHibernateDAO?

I suspect that you might create it using 'new'. That is not correct. Use
Tapestry injections instead, e.g. in your page or component:

@Inject
private BranchDAO dao;

Regards,
Cezary

Re: session is NULL

Posted by Anas Mughal <an...@yahoo.com>.
I initially had a constructor that accepted a session. That worked fine.

Then, I took out the constructor and simply injected the session. That is working fine as well.

Now, I am injecting my delegate as service into my page. The DAO is injected to my delegate. Then, the session is injected into my DAO. That is working well for me. (No constructors.)

Best Regards. 
--Anas Mughal
http://anas-mughal.com 
     

--- On Thu, 11/11/10, Everton Agner <to...@gmail.com> wrote:

From: Everton Agner <to...@gmail.com>
Subject: Re: session is NULL
To: "Tapestry users" <us...@tapestry.apache.org>
Date: Thursday, November 11, 2010, 8:23 AM

> @Inject is for pages. Make your DAO a Tapestry service and inject the
> session to it via its constructor.
>
> Kalle

I guess not... Since the DAO is a service, you can either @Inject fields,
pass thru construtuctor call or build them.

_______________________
Everton Agner Ramos


2010/11/10 Anas Mughal <an...@yahoo.com>

> Yes, that worked. I wish to thank everyone for their help!
>
> Tapestry is great!
> Coming from the Spring world, I do not miss those verbose Spring
> configuration files!
>
> --Anas Mughal
> http://anas-mughal.com
>
>
> --- On Wed, 11/10/10, Rich M <ri...@moremagic.com> wrote:
>
> From: Rich M <ri...@moremagic.com>
> Subject: Re: session is NULL
> To: "Tapestry users" <us...@tapestry.apache.org>
> Date: Wednesday, November 10, 2010, 10:34 AM
>
> On 11/10/2010 03:42 AM, Anas Mughal wrote:
> > I have setup my DAO as a Tapestry service in the AppModule as:
> >      public class AppModule
> >    {
> >       public static void bind(ServiceBinder binder)
> >       {
> >         binder.bind(BranchDAO.class, BranchHibernateDAO.class);
> >
> >       }
> >    }
> >     Then, I try to inject the session as follows:
> >        public class BranchHibernateDAO implements BranchDAO {
> >
> >       @Inject
> >       private Session session;
> >
> >
> >            @SuppressWarnings("unchecked")
> >       public Object find( Class c , BigDecimal id)
> >       {
> >         return session.get(c, id);
> >       }
> >
> >    }
> >
> > I still get a NULL session.
> >
> >
> >
> >
> >
> >
> >
> > Following the suggestion by Kalle, I injected the session to a page.
> Then, passed the session to my DAO in the contructor. That worked! However,
> I would rather keep the code cleaner by injecting the session directly into
> the DAO.
> >
> To use the DAO, you will want the following setup.
>
> public class BranchHibernateDAO implements BranchDAO {
>
>     private Session session;
>
>     public BranchHibernateDAO(Session session){
>
>         this.session = session;
>
>     }
>
>     ...
>
> }
>
>
> then in your page class:
>
> @Inject
>
> private BranchHibernateDAO bhdao;
>
>
> You do not need to initialize the BranchHibernateDAO yourself. Tapestry-IoC
> will initialize the BranchHibernateDAO lazily behind the scenes the first
> time you access it in the web application. In this case, that would be when
> you navigate to the page relating to your page class, where the DAO is
> injected.
>
> The convention for services, is that when they are setup in the bind method
> of the AppModule (or whatever your module is called), Tapestry will "inject"
> the appropriate objects to its constructor when it initializes the service.
>
> This convention will keep your page classes cleaner.
> >   Any suggestion would be greatly appreciated.
> > --- On Wed, 11/10/10, Kalle Korhonen<ka...@gmail.com>  wrote:
> >
> >
> > From: Kalle Korhonen<ka...@gmail.com>
> > Subject: Re: session is NULL
> > To: "Tapestry users"<us...@tapestry.apache.org>
> > Date: Wednesday, November 10, 2010, 1:48 AM
> >
> >
> > @Inject is for pages. Make your DAO a Tapestry service and inject the
> > session to it via its constructor.
> >
> > Kalle
> >
> >
> > On Tue, Nov 9, 2010 at 9:52 PM, Anas Mughal<an...@yahoo.com>
> wrote:
> >
> >> I have setup my Tapestry project using the Maven archetype. Then, I
> setup my hibernate.cfg.xml file with references to my hibernate mapping
> files. (I am not using annotations for hibernate.)
> >>
> >> Now, I have setup a simple DAO object to try to retrieve an object from
> the database:
> >>
> >> public class BranchDAO {
> >>
> >>      @Inject
> >>      private Session session;
> >>
> >>      @SuppressWarnings("unchecked")
> >>      public Object find( Class c , BigDecimal id)
> >>      {
> >>          return  session.get(c, id);  // session is NULL here
> >>      }
> >> }
> >>
> >>
> >> I get a NULL pointer exception because my session does not seem to be
> >>   initialized.
> >>
> >> Searching online, I came accross:
> >> http://wiki.apache.org/tapestry/SessionPagePersistence
> >>
> >> I have not setup any hivemind configuration or any of the suggested
> classes on that wiki page. Please advise me what do I need to be able to
> fetch objects using Tapestry-Hibernate. I don't know where to place the
> hivemind configuration file -- if I need it.
> >>
> >> I am new to Tapestry. Please bear with me.
> >>
> >> Thank you for your kind assistance.
> >> --Anas Mughal
> >> http://anas-mughal.com
> >>
> >>
> >>
> >>
> >>
> >>
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
>
>
>



      

Re: session is NULL

Posted by Everton Agner <to...@gmail.com>.
> @Inject is for pages. Make your DAO a Tapestry service and inject the
> session to it via its constructor.
>
> Kalle

I guess not... Since the DAO is a service, you can either @Inject fields,
pass thru construtuctor call or build them.

_______________________
Everton Agner Ramos


2010/11/10 Anas Mughal <an...@yahoo.com>

> Yes, that worked. I wish to thank everyone for their help!
>
> Tapestry is great!
> Coming from the Spring world, I do not miss those verbose Spring
> configuration files!
>
> --Anas Mughal
> http://anas-mughal.com
>
>
> --- On Wed, 11/10/10, Rich M <ri...@moremagic.com> wrote:
>
> From: Rich M <ri...@moremagic.com>
> Subject: Re: session is NULL
> To: "Tapestry users" <us...@tapestry.apache.org>
> Date: Wednesday, November 10, 2010, 10:34 AM
>
> On 11/10/2010 03:42 AM, Anas Mughal wrote:
> > I have setup my DAO as a Tapestry service in the AppModule as:
> >      public class AppModule
> >    {
> >       public static void bind(ServiceBinder binder)
> >       {
> >         binder.bind(BranchDAO.class, BranchHibernateDAO.class);
> >
> >       }
> >    }
> >     Then, I try to inject the session as follows:
> >        public class BranchHibernateDAO implements BranchDAO {
> >
> >       @Inject
> >       private Session session;
> >
> >
> >            @SuppressWarnings("unchecked")
> >       public Object find( Class c , BigDecimal id)
> >       {
> >         return session.get(c, id);
> >       }
> >
> >    }
> >
> > I still get a NULL session.
> >
> >
> >
> >
> >
> >
> >
> > Following the suggestion by Kalle, I injected the session to a page.
> Then, passed the session to my DAO in the contructor. That worked! However,
> I would rather keep the code cleaner by injecting the session directly into
> the DAO.
> >
> To use the DAO, you will want the following setup.
>
> public class BranchHibernateDAO implements BranchDAO {
>
>     private Session session;
>
>     public BranchHibernateDAO(Session session){
>
>         this.session = session;
>
>     }
>
>     ...
>
> }
>
>
> then in your page class:
>
> @Inject
>
> private BranchHibernateDAO bhdao;
>
>
> You do not need to initialize the BranchHibernateDAO yourself. Tapestry-IoC
> will initialize the BranchHibernateDAO lazily behind the scenes the first
> time you access it in the web application. In this case, that would be when
> you navigate to the page relating to your page class, where the DAO is
> injected.
>
> The convention for services, is that when they are setup in the bind method
> of the AppModule (or whatever your module is called), Tapestry will "inject"
> the appropriate objects to its constructor when it initializes the service.
>
> This convention will keep your page classes cleaner.
> >   Any suggestion would be greatly appreciated.
> > --- On Wed, 11/10/10, Kalle Korhonen<ka...@gmail.com>  wrote:
> >
> >
> > From: Kalle Korhonen<ka...@gmail.com>
> > Subject: Re: session is NULL
> > To: "Tapestry users"<us...@tapestry.apache.org>
> > Date: Wednesday, November 10, 2010, 1:48 AM
> >
> >
> > @Inject is for pages. Make your DAO a Tapestry service and inject the
> > session to it via its constructor.
> >
> > Kalle
> >
> >
> > On Tue, Nov 9, 2010 at 9:52 PM, Anas Mughal<an...@yahoo.com>
> wrote:
> >
> >> I have setup my Tapestry project using the Maven archetype. Then, I
> setup my hibernate.cfg.xml file with references to my hibernate mapping
> files. (I am not using annotations for hibernate.)
> >>
> >> Now, I have setup a simple DAO object to try to retrieve an object from
> the database:
> >>
> >> public class BranchDAO {
> >>
> >>      @Inject
> >>      private Session session;
> >>
> >>      @SuppressWarnings("unchecked")
> >>      public Object find( Class c , BigDecimal id)
> >>      {
> >>          return  session.get(c, id);  // session is NULL here
> >>      }
> >> }
> >>
> >>
> >> I get a NULL pointer exception because my session does not seem to be
> >>   initialized.
> >>
> >> Searching online, I came accross:
> >> http://wiki.apache.org/tapestry/SessionPagePersistence
> >>
> >> I have not setup any hivemind configuration or any of the suggested
> classes on that wiki page. Please advise me what do I need to be able to
> fetch objects using Tapestry-Hibernate. I don't know where to place the
> hivemind configuration file -- if I need it.
> >>
> >> I am new to Tapestry. Please bear with me.
> >>
> >> Thank you for your kind assistance.
> >> --Anas Mughal
> >> http://anas-mughal.com
> >>
> >>
> >>
> >>
> >>
> >>
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
>
>
>

Re: session is NULL

Posted by Anas Mughal <an...@yahoo.com>.
Yes, that worked. I wish to thank everyone for their help! 
 
Tapestry is great!
Coming from the Spring world, I do not miss those verbose Spring configuration files!

--Anas Mughal 
http://anas-mughal.com 
    

--- On Wed, 11/10/10, Rich M <ri...@moremagic.com> wrote:

From: Rich M <ri...@moremagic.com>
Subject: Re: session is NULL
To: "Tapestry users" <us...@tapestry.apache.org>
Date: Wednesday, November 10, 2010, 10:34 AM

On 11/10/2010 03:42 AM, Anas Mughal wrote:
> I have setup my DAO as a Tapestry service in the AppModule as:
>      public class AppModule
>    {
>       public static void bind(ServiceBinder binder)
>       {
>         binder.bind(BranchDAO.class, BranchHibernateDAO.class);
> 
>       }
>    }
>     Then, I try to inject the session as follows:
>        public class BranchHibernateDAO implements BranchDAO {
> 
>       @Inject
>       private Session session;
> 
> 
>            @SuppressWarnings("unchecked")
>       public Object find( Class c , BigDecimal id)
>       {
>         return session.get(c, id);
>       }
> 
>    }
>   
> I still get a NULL session.
>     
> 
> 
> 
> 
> 
> 
> Following the suggestion by Kalle, I injected the session to a page. Then, passed the session to my DAO in the contructor. That worked! However, I would rather keep the code cleaner by injecting the session directly into the DAO.
>    
To use the DAO, you will want the following setup.

public class BranchHibernateDAO implements BranchDAO {

    private Session session;

    public BranchHibernateDAO(Session session){

        this.session = session;

    }

    ...

}


then in your page class:

@Inject

private BranchHibernateDAO bhdao;


You do not need to initialize the BranchHibernateDAO yourself. Tapestry-IoC will initialize the BranchHibernateDAO lazily behind the scenes the first time you access it in the web application. In this case, that would be when you navigate to the page relating to your page class, where the DAO is injected.

The convention for services, is that when they are setup in the bind method of the AppModule (or whatever your module is called), Tapestry will "inject" the appropriate objects to its constructor when it initializes the service.

This convention will keep your page classes cleaner.
>   Any suggestion would be greatly appreciated.               
> --- On Wed, 11/10/10, Kalle Korhonen<ka...@gmail.com>  wrote:
> 
> 
> From: Kalle Korhonen<ka...@gmail.com>
> Subject: Re: session is NULL
> To: "Tapestry users"<us...@tapestry.apache.org>
> Date: Wednesday, November 10, 2010, 1:48 AM
> 
> 
> @Inject is for pages. Make your DAO a Tapestry service and inject the
> session to it via its constructor.
> 
> Kalle
> 
> 
> On Tue, Nov 9, 2010 at 9:52 PM, Anas Mughal<an...@yahoo.com>  wrote:
>    
>> I have setup my Tapestry project using the Maven archetype. Then, I setup my hibernate.cfg.xml file with references to my hibernate mapping files. (I am not using annotations for hibernate.)
>> 
>> Now, I have setup a simple DAO object to try to retrieve an object from the database:
>> 
>> public class BranchDAO {
>> 
>>      @Inject
>>      private Session session;
>> 
>>      @SuppressWarnings("unchecked")
>>      public Object find( Class c , BigDecimal id)
>>      {
>>          return  session.get(c, id);  // session is NULL here
>>      }
>> }
>> 
>> 
>> I get a NULL pointer exception because my session does not seem to be
>>   initialized.
>> 
>> Searching online, I came accross:
>> http://wiki.apache.org/tapestry/SessionPagePersistence
>> 
>> I have not setup any hivemind configuration or any of the suggested classes on that wiki page. Please advise me what do I need to be able to fetch objects using Tapestry-Hibernate. I don't know where to place the hivemind configuration file -- if I need it.
>> 
>> I am new to Tapestry. Please bear with me.
>> 
>> Thank you for your kind assistance.
>> --Anas Mughal
>> http://anas-mughal.com
>> 
>> 
>> 
>> 
>> 
>>      
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 
> 
> 
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org




      

Re: session is NULL

Posted by Rich M <ri...@moremagic.com>.
On 11/10/2010 03:42 AM, Anas Mughal wrote:
> I have setup my DAO as a Tapestry service in the AppModule as:
>   
>    public class AppModule
>    {
>       public static void bind(ServiceBinder binder)
>       {
>         binder.bind(BranchDAO.class, BranchHibernateDAO.class);
>
>       }
>    }
>   
>   
> Then, I try to inject the session as follows:
>   
>   
>    public class BranchHibernateDAO implements BranchDAO {
>
>       @Inject
>       private Session session;
>
>
>      
>       @SuppressWarnings("unchecked")
>       public Object find( Class c , BigDecimal id)
>       {
>         return session.get(c, id);
>       }
>
>    }
>   
>
> I still get a NULL session.
>   
>   
>
>
>
>
>
>
>
> Following the suggestion by Kalle, I injected the session to a page. Then, passed the session to my DAO in the contructor. That worked! However, I would rather keep the code cleaner by injecting the session directly into the DAO.
>    
To use the DAO, you will want the following setup.

public class BranchHibernateDAO implements BranchDAO {

     private Session session;

     public BranchHibernateDAO(Session session){

         this.session = session;

     }

     ...

}


then in your page class:

@Inject

private BranchHibernateDAO bhdao;


You do not need to initialize the BranchHibernateDAO yourself. 
Tapestry-IoC will initialize the BranchHibernateDAO lazily behind the 
scenes the first time you access it in the web application. In this 
case, that would be when you navigate to the page relating to your page 
class, where the DAO is injected.

The convention for services, is that when they are setup in the bind 
method of the AppModule (or whatever your module is called), Tapestry 
will "inject" the appropriate objects to its constructor when it 
initializes the service.

This convention will keep your page classes cleaner.
>   
> Any suggestion would be greatly appreciated. 
>   
>   
>   
>   
>   
>   
>   
>
> --- On Wed, 11/10/10, Kalle Korhonen<ka...@gmail.com>  wrote:
>
>
> From: Kalle Korhonen<ka...@gmail.com>
> Subject: Re: session is NULL
> To: "Tapestry users"<us...@tapestry.apache.org>
> Date: Wednesday, November 10, 2010, 1:48 AM
>
>
> @Inject is for pages. Make your DAO a Tapestry service and inject the
> session to it via its constructor.
>
> Kalle
>
>
> On Tue, Nov 9, 2010 at 9:52 PM, Anas Mughal<an...@yahoo.com>  wrote:
>    
>> I have setup my Tapestry project using the Maven archetype. Then, I setup my hibernate.cfg.xml file with references to my hibernate mapping files. (I am not using annotations for hibernate.)
>>
>> Now, I have setup a simple DAO object to try to retrieve an object from the database:
>>
>> public class BranchDAO {
>>
>>      @Inject
>>      private Session session;
>>
>>      @SuppressWarnings("unchecked")
>>      public Object find( Class c , BigDecimal id)
>>      {
>>          return  session.get(c, id);  // session is NULL here
>>      }
>> }
>>
>>
>> I get a NULL pointer exception because my session does not seem to be
>>   initialized.
>>
>> Searching online, I came accross:
>> http://wiki.apache.org/tapestry/SessionPagePersistence
>>
>> I have not setup any hivemind configuration or any of the suggested classes on that wiki page. Please advise me what do I need to be able to fetch objects using Tapestry-Hibernate. I don't know where to place the hivemind configuration file -- if I need it.
>>
>> I am new to Tapestry. Please bear with me.
>>
>> Thank you for your kind assistance.
>> --Anas Mughal
>> http://anas-mughal.com
>>
>>
>>
>>
>>
>>      
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
>
>
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: session is NULL

Posted by Anas Mughal <an...@yahoo.com>.
I have setup my DAO as a Tapestry service in the AppModule as:
 
  public class AppModule
  {
     public static void bind(ServiceBinder binder)
     {
       binder.bind(BranchDAO.class, BranchHibernateDAO.class);

     }
  }
 
 
Then, I try to inject the session as follows:
 
 
  public class BranchHibernateDAO implements BranchDAO {

     @Inject
     private Session session;


    
     @SuppressWarnings("unchecked")
     public Object find( Class c , BigDecimal id)
     {
       return session.get(c, id);
     }

  }
 

I still get a NULL session.
 
 







Following the suggestion by Kalle, I injected the session to a page. Then, passed the session to my DAO in the contructor. That worked! However, I would rather keep the code cleaner by injecting the session directly into the DAO. 
 
Any suggestion would be greatly appreciated. 
 
 
 
 
 
 
 

--- On Wed, 11/10/10, Kalle Korhonen <ka...@gmail.com> wrote:


From: Kalle Korhonen <ka...@gmail.com>
Subject: Re: session is NULL
To: "Tapestry users" <us...@tapestry.apache.org>
Date: Wednesday, November 10, 2010, 1:48 AM


@Inject is for pages. Make your DAO a Tapestry service and inject the
session to it via its constructor.

Kalle


On Tue, Nov 9, 2010 at 9:52 PM, Anas Mughal <an...@yahoo.com> wrote:
> I have setup my Tapestry project using the Maven archetype. Then, I setup my hibernate.cfg.xml file with references to my hibernate mapping files. (I am not using annotations for hibernate.)
>
> Now, I have setup a simple DAO object to try to retrieve an object from the database:
>
> public class BranchDAO {
>
>     @Inject
>     private Session session;
>
>     @SuppressWarnings("unchecked")
>     public Object find( Class c , BigDecimal id)
>     {
>         return  session.get(c, id);  // session is NULL here
>     }
> }
>
>
> I get a NULL pointer exception because my session does not seem to be
>  initialized.
>
> Searching online, I came accross:
> http://wiki.apache.org/tapestry/SessionPagePersistence
>
> I have not setup any hivemind configuration or any of the suggested classes on that wiki page. Please advise me what do I need to be able to fetch objects using Tapestry-Hibernate. I don't know where to place the hivemind configuration file -- if I need it.
>
> I am new to Tapestry. Please bear with me.
>
> Thank you for your kind assistance.
> --Anas Mughal
> http://anas-mughal.com
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org




      

Re: session is NULL

Posted by Kalle Korhonen <ka...@gmail.com>.
@Inject is for pages. Make your DAO a Tapestry service and inject the
session to it via its constructor.

Kalle


On Tue, Nov 9, 2010 at 9:52 PM, Anas Mughal <an...@yahoo.com> wrote:
> I have setup my Tapestry project using the Maven archetype. Then, I setup my hibernate.cfg.xml file with references to my hibernate mapping files. (I am not using annotations for hibernate.)
>
> Now, I have setup a simple DAO object to try to retrieve an object from the database:
>
> public class BranchDAO {
>
>     @Inject
>     private Session session;
>
>     @SuppressWarnings("unchecked")
>     public Object find( Class c , BigDecimal id)
>     {
>         return  session.get(c, id);  // session is NULL here
>     }
> }
>
>
> I get a NULL pointer exception because my session does not seem to be
>  initialized.
>
> Searching online, I came accross:
> http://wiki.apache.org/tapestry/SessionPagePersistence
>
> I have not setup any hivemind configuration or any of the suggested classes on that wiki page. Please advise me what do I need to be able to fetch objects using Tapestry-Hibernate. I don't know where to place the hivemind configuration file -- if I need it.
>
> I am new to Tapestry. Please bear with me.
>
> Thank you for your kind assistance.
> --Anas Mughal
> http://anas-mughal.com
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org