Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: getHibernateTemplate() returns null

  1. #1
    Join Date
    Nov 2004
    Posts
    29

    Default getHibernateTemplate() returns null

    Hi ,

    I've been trying to implement some Spring ORM features in my Hibernate DAO's . After reading Bruce Tate's book on better / lighter java , and looking at the docs linked at the spring documentation page I came up with the following changes to implement in my Tapestry / Hibernate app :

    Code:
    public class IdsDAOImpl extends HibernateDaoSupport implements IdsDAO  {
    
        private SessionFactory sessionFactory;
    
        /* Commented because it's overriden in 2 examples , yet final in the  spring source 
       public void setSessionFactory(SessionFactory sessionFactory) {
                 this.sessionFactory = sessionFactory;
        } */
    
            public IdsDAOImpl( ) {
            }
    
            public void someWritterMethod () { 
              HibernateTemplate ht  = getHibernateTemplate(); ... ...
    i've added an applicationContext.xml file in my WEB-INF
    Code:
    <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName">
            <value>jdbc/frd</value> 
          </property>
        </bean>
    
        <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
          <property name="mappingResources">
            <list>
              ...
              <value>Ids.hbm.xml</value>
            </list>
          </property>
          <property name="hibernateProperties">
            <props>
              <prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
            </props>
          </property>
          <property name="dataSource">
            <ref bean="myDataSource"/>
          </property>
        </bean>
    ...       
        <bean id="idsDAO" class="sic.freddy.dao.hibernate.IdsDAOImpl">
            <property name="sessionFactory">
            <ref bean="mySessionFactory"/>
            </property>
        </bean>
    modified web.xml to see the applicationContext.xml
    Code:
    <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/applicationContext.xml
    		</param-value>
    		</context-param>
    When trying to run a test that accesses the someWritterMethod() , tomcat throws an NullPointerException at the line that I try to work with the HibernateTemplate object . I tested it and the object is null . No other exceptions are shown y Spring or Hibernate while deploying or running .

    Any ideeas / sugestions are greately apreciated .

  2. #2
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    You shouldn't add a sessionFactory your your class. There is already one in the HIbernateaoSupport and yours is hiding the one which is set in the configuration. Remove your property and let the hibernateDaoSupport inheritance add the setters and do the cook behind the scene.

    HTH

    Olivier

  3. #3
    Join Date
    Nov 2004
    Posts
    29

    Default

    thanks for the reply ,

    i tried your sugestion , and that didn't seem to fix it . I proceded to also coment out the blank constructor i've created following some of the examples , but also with no luck .

    Anything else I might be missing ?

  4. #4
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    hmm, you may try to directly inject an HibernateTemplate instead of a SessionFactory. You can create an HibernateTemplate with just a SessionFactory in its constructor.
    This is what is done in the HibernateDaoSupport constructor ...


    Olivier

  5. #5
    Join Date
    Nov 2004
    Posts
    29

    Default

    makes sense , however it will take me some time to sort out through the code and documentation , i just started playing with spring .

    i'll still do it , but , if anyone has any other thoughts , i'd like to get an answer about why it's not working the normal way , so if not in this one , maybe in future implementations i can use that way

    thanks

  6. #6
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    gigi,
    did you look at petclinic sample from Spring distribution and more specifically src/org/springframework/samples/petclinic/hibernate/HibernateClinic.java? It shows how to use HibernateDaoSupport.
    no SessionFactory member / setters / getters need to be added. It just works out of the box.
    the following lines are from war/WEB-INF/applicationContext-hibernate.xml
    Code:
    	<bean id="clinicTarget" class="org.springframework.samples.petclinic.hibernate.HibernateClinic">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    
    	<bean id="clinic" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="transactionManager"/></property>
    		<property name="target"><ref local="clinicTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="store*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  7. #7
    Join Date
    Nov 2004
    Posts
    29

    Default

    Thanks , yes , i've gotten rid of all of the setters and sessionFactry declarations from my code , but that did not help the problem . I'm gonna start analyzing things again , and see where my code has a problem .

    I'll also look in the docs for the TransactionProxyFactoryBean as i've never used it

  8. #8
    Join Date
    Nov 2004
    Posts
    29

    Default

    but the question here still remains ...

    why isn't the session factory beeing initialised , even though i have declared it as a bean in the aplicatoinContext.xml .

    The problem is that , no exception is thrown anywhere , to indicate that , the sessionFactory isn't beeing initialised or why ... where should i be looking for this ?

  9. #9
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Could you turn logging level to debug and post the result?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  10. #10
    Join Date
    Nov 2004
    Posts
    29

    Default

    hi , i'm sorry for beeing such a "new user" but as i turn on :
    log4j.rootLogger=debug , stdout
    the tomcat console gets filled with Tapestry messages . nothing whatsoever about hibernate or spring
    i tried adding this :
    log4j.logger.org.apahe.tapestry= warn
    but that didn't stop tapestry from flooding the console

    edit : no , looks like tapestry messages are in warn mode , after lots of messages first time compiling the presentation file , tapestry shuts up and every test i do , does not flood the console anymore . however , still no hibernate or spring messages

Similar Threads

  1. Replies: 2
    Last Post: Oct 17th, 2005, 08:41 PM
  2. Replies: 2
    Last Post: Oct 13th, 2005, 02:47 PM
  3. Replies: 4
    Last Post: Sep 27th, 2005, 11:31 PM
  4. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  5. Strange Data Access Error
    By webifyit in forum Data
    Replies: 2
    Last Post: Dec 28th, 2004, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •