[Building Sakai] Unable to getHibernate template

Aaron Zeckoski aaronz at vt.edu
Fri Apr 10 03:00:41 PDT 2009


OK, here is the problem in the code. You are not using dependency
injection to get the service (or dao in your case) into your tool
code. So, this code in PersonTool is not going to work for a few
reasons:
if(personManager==null)	
{
	System.out.println("in if(personManager==null) check");
	personManager = new PersonManagerImpl();
	if(personManager==null)	
	{
		System.out.println("in if(personManager==null) check again");
	}
	else
	{
		personManager.savePerson(person);
	}
}

1) It will never do a save unless the personManager is null (which it
won't be once you fix the DI)
2) You are creating the DAO (PersonManagerImpl) using new when you
need to actually use the one which was created by spring (because of
your components.xml file)
3) an object which is created using a constructor can never be null so
the check for null after creating the object is not needed and is
confusing
4) This is not the problem here but you should no use the IMPL in this
class, use the interface instead

To fix the issue, you would change the code above to:
personManager.savePerson(person);

And in your faces-config you would need to add something like this:
<managed-bean>
....
   <managed-bean-class>org.sakaiproject.tool.person.PersonTool</managed-bean-class>
....
                <managed-property>
                        <description>personManager</description>
                        <property-name>personManager</property-name>

<value>#{Components["org.sakaiproject.api.app.person.PersonManager"]}</value>
                </managed-property>
</managed-bean>

That will inject the PersonManager into the tool via the setter for
PersonManager.
-AZ


On Fri, Apr 10, 2009 at 10:44 AM, Ketan Nale <ketan_nale at yahoo.com> wrote:
> ok thanks
> i am also switching from JSF to our traditional JSP.
> and i will get more familiar with the concept of dependency
> injection
> but please tell me what you wrong you pointed in my code, i cant go with
> this suspense now...
>
> thanks,
> ketan
>
>>> --- On Thu, 9/4/09, Aaron Zeckoski <aaronz at vt.edu> wrote:
>>>
>>> From: Aaron Zeckoski <aaronz at vt..edu>
>>> Subject: Re: [Building Sakai] Unable to getHibernate template
>>> To: "Ketan Nale" <ketan_nale at yahoo.com>
>>> Date: Thursday, 9 April, 2009, 7:57 PM
>>>
>>> OK, how are you accessing this PersonManager? I don't see anything in
>>> your components.xml file which would indicate this. My guess is that
>>> the session is not being set and this is why you are getting a null
>>> back. If you can send me the spring file and the code where you are
>>> attempting to use the PersonManager (the whole file) then I will try
>>> to figure out why it is not being set.
>>>
>>> -AZ
>>>
>>>
>>> On Thu, Apr 9, 2009 at 2:49 PM, Ketan Nale <ketan_nale at yahoo.com> wrote:
>>>> ya why not it is attached to this mail
>>>>
>>>> --- On Thu, 9/4/09, Aaron Zeckoski <aaronz at vt.edu> wrote:
>>>>
>>>> From: Aaron Zeckoski <aaronz at vt..edu>
>>>> Subject: Re: [Building Sakai] Unable to getHibernate template
>>>> To: "Ketan Nale" <ketan_nale at yahoo.com>
>>>> Date: Thursday, 9 April, 2009, 6:44 PM
>>>>
>>>> Also, can you send me the PersonManagerImpl file?
>>>>
>>>> On Thu, Apr 9, 2009 at 1:51 PM, Ketan Nale <ketan_nale at yahoo.com> wrote:
>>>>>
>>>>>
>>>>> This is the simple whole function ::
>>>>>
>>>>> public void savePerson(Person person) {
>>>>>         // TODO Auto-generated method stub
>>>>>         System.out.println("in public void savePerson(Person person) of
>>>>> PersonManagerImpl");
>>>>>
>>>>>         System.out.println("getHibernateTemplate() "+
>>>>> getHibernateTemplate());
>>>>>
>>>>>
>>>>>         try {
>>>>>             getHibernateTemplate().saveOrUpdate(person);   // this is
>>>>> line
>>>>> 65
>>>>>         } catch (Exception e) {
>>>>>             // TODO: handle exception
>>>>>
>>>>> System.out.println("------------------------------------------------");
>>>>>
>>>>> System.out.println("--------getHibernateTemplate()stacktrace--------");
>>>>>
>>>>> System.out.println("--------------------START-----------------------");
>>>>>             e..printStackTrace();
>>>>>
>>>>> System.out.println("---------------------END------------------------");
>>>>>
>>>>> System.out.println("--------getHibernateTemplate()stacktrace--------");
>>>>>
>>>>> System.out.println("------------------------------------------------");
>>>>>         }
>>>>>     }
>>>>>
>>>>> please find the attachment of components.xml
>>>>>
>>>>> --- On Thu, 9/4/09, Aaron Zeckoski <aaronz at vt.edu> wrote:
>>>>>
>>>>> From: Aaron Zeckoski <aaronz at vt...edu>
>>>>> Subject: Re: [Building Sakai] Unable to getHibernate template
>>>>> To: "KetanNale" <ketan_nale at yahoo.com>
>>>>> Cc: sakai-dev at collab.sakaiproject..org
>>>>> Date: Thursday, 9 April, 2009, 6:12 PM
>>>>>
>>>>> Can you send the few lines before, after, and including this one?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> org.sakaiproject.api.app.person.PersonManagerImpl.savePerson(PersonManagerImpl.java:65)
>>>>> and mark like 65
>>>>>
>>>>> Also, please attach your components.xml file.
>>>>> -AZ
>>>>>
>>>>>
>>>>> On Thu, Apr 9, 2009 at 1:34 PM, KetanNale <ketan_nale at yahoo.com> wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>> Hello,
>>>>>>
>>>>>>  i have problem in getting object of getHibernateTemplate() in my
>>>>>> manager.
>>>>>> Please anyone can tell me where is the problem ?
>>>>>>
>>>>>> Sir i have tried 3rd option as per my requirement of following link
>>>>>> http://bugs.sakaiproject.org/confluence/display/BOOT/Persistence
>>>>>>
>>>>>> also in dependencies of myapp-impl  (i.e. in pom..xml)
>>>>>>
>>>>>> following entry is there
>>>>>>
>>>>>> <dependency>
>>>>>>      <groupId>org.hibernate</groupId>
>>>>>>      <artifactId>hibernate</artifactId>
>>>>>>    </dependency>
>>>>>>
>>>>>>    <dependency>
>>>>>>      <groupId>org.springframework</groupId>
>>>>>>      <artifactId>spring</artifactId>
>>>>>>    </dependency>
>>>>>>
>>>>>> also i have added spring.jar in classpath
>>>>>> but still cant get rid of my problem
>>>>>>
>>>>>> this time putting stacktrace  below
>>>>>> if you want any specific file please let me know
>>>>>>
>>>>>> thanks,
>>>>>> ketan
>>>>>> ------------------------------------------------
>>>>>> --------getHibernateTemplate()stacktrace--------
>>>>>> --------------------START-----------------------
>>>>>> java.lang.NullPointerException
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.api.app.person.PersonManagerImpl.savePerson(PersonManagerImpl.java:65)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.tool.person.PersonTool.processSave(PersonTool.java:134)
>>>>>>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>>>>>        at java.lang.reflect.Method.invoke(Method.java:585)
>>>>>>        at
>>>>>> com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> com..sun...faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
>>>>>>        at
>>>>>> javax..faces.component.UICommand.broadcast(UICommand.java:312)
>>>>>>        at
>>>>>> javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>> javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> com.sun.faces..lifecycle.InvokeApplicationPhase..execute(InvokeApplicationPhase.java:75)
>>>>>>        at
>>>>>> com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
>>>>>>        at
>>>>>> com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
>>>>>>        at
>>>>>> javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
>>>>>>        at
>>>>>> org.sakaiproject.util.RequestFilter.doFilter(RequestFilter.java:561)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
>>>>>>        at
>>>>>> org.sakaiproject..jsf.util.JsfTool.dispatch(JsfTool.java:221)
>>>>>>        at org.sakaiproject.jsf.util.JsfTool.doPost(JsfTool.java:256)
>>>>>>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
>>>>>>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
>>>>>>        at
>>>>>> org.sakaiproject..util.RequestFilter.doFilter(RequestFilter..java:561)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache..catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache..catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache..catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:364)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.tool.impl.ActiveToolComponent$MyActiveTool.forward(ActiveToolComponent..java:475)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.portal.charon.SkinnableCharonPortal.forwardTool(SkinnableCharonPortal.java:1250)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.portal.charon.handlers.ToolHandler.doTool(ToolHandler.java:188)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.portal.charon.handlers.ToolHandler.doGet(ToolHandler.java:93)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.portal.charon.handlers.ToolHandler..doPost(ToolHandler.java:70)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.sakaiproject.portal.charon.SkinnableCharonPortal.doPost(SkinnableCharonPortal.java:1093)
>>>>>>        at
>>>>>> javax.servlet.http.HttpServlet.service(HttpServlet...java:647)
>>>>>>        at
>>>>>> javax..servlet.http.HttpServlet.service(HttpServlet.java:729)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
>>>>>>        at
>>>>>> org.sakaiproject.util.RequestFilter.doFilter(RequestFilter.java:613)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.valves.ErrorReportValve..invoke(ErrorReportValve..java:117)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.coyote..http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
>>>>>>
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.tomcat..util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache..tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
>>>>>>        at
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
>>>>>>        at java.lang.Thread.run(Thread.java:595)
>>>>>> ---------------------END------------------------
>>>>>> --------getHibernateTemplate()stacktrace--------
>>>>>> ------------------------------------------------
>>>>>>
>>>>>> --
>>>>>> View this message in context:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> http://www..nabble.com/Unable-to-getHibernate-template-tp22968135p22970023.html
>>>>>> Sent from the Sakai - Development mailing list archive at Nabble.com..
>>>>>>
>>>>>> _______________________________________________
>>>>>> sakai-dev mailing list
>>>>>> sakai-dev at collab.sakaiproject.org
>>>>>> http://collab.sakaiproject.org/mailman/listinfo/sakai-dev
>>>>>>
>>>>>> TO UNSUBSCRIBE: send email to
>>>>>> sakai-dev-unsubscribe at collab.sakaiproject.org with a subject of
>>>>>> "unsubscribe"
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Aaron Zeckoski (aaronz at vt.edu)
>>>>> Senior Research Engineer - CARET - Cambridge University
>>>>> [http://bugs.sakaiproject.org/confluence/display/~aaronz/]
>>>>> Sakai Fellow - [http://aaronz-sakai.blogspot.com/]
>>>>>
>>>>> ________________________________
>>>>> Add more friends to your messenger and enjoy! Invite them now.
>>>>
>>>>
>>>>
>>>> --
>>>> Aaron Zeckoski (aaronz at vt.edu)
>>>> Senior Research Engineer - CARET - Cambridge University
>>>> [http://bugs.sakaiproject.org/confluence/display/~aaronz/]
>>>> Sakai Fellow - [http://aaronz-sakai.blogspot..com/]
>>>>
>>>> ________________________________
>>>> Bollywood news, movie reviews, film trailers and more! Click here.
>>>
>>>
>>>
>>> --
>>> Aaron Zeckoski (aaronz at vt..edu)
>>> Senior Research Engineer - CARET - Cambridge University
>>> [http://bugs.sakaiproject.org/confluence/display/~aaronz/]
>>> Sakai Fellow - [http://aaronz-sakai.blogspot.com/]
>>>
>>> ________________________________
>>> Add more friends to your messenger and enjoy! Invite them now.
>>
>>
>>
>> --
>> Aaron Zeckoski (aaronz at vt.edu)
>> Senior Research Engineer - CARET - Cambridge University
>> [http://bugs.sakaiproject.org/confluence/display/~aaronz/]
>> Sakai Fellow - [http://aaronz-sakai.blogspot.com/]
>>
>> ________________________________
>> Add more friends to your messenger and enjoy! Invite them now.
>
>
>
> --
> Aaron Zeckoski (aaronz at vt.edu)
> Senior Research Engineer - CARET - Cambridge University
> [http://bugs.sakaiproject.org/confluence/display/~aaronz/]
> Sakai Fellow - [http://aaronz-sakai.blogspot.com/]
>
> ________________________________
> Add more friends to your messenger and enjoy! Invite them now.



-- 
Aaron Zeckoski (aaronz at vt.edu)
Senior Research Engineer - CARET - Cambridge University
[http://bugs.sakaiproject.org/confluence/display/~aaronz/]
Sakai Fellow - [http://aaronz-sakai.blogspot.com/]


More information about the sakai-dev mailing list