Announcement Announcement Module
Collapse
No announcement yet.
HibernateDaoSupport.getSessionFactory() returns null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateDaoSupport.getSessionFactory() returns null

    And I cannot work out why.

    I have a been declared in my application context
    <bean id="basicDao" class="com.discomanifesto.orm.BasicDao">
    <property name="sessionFactory" ref="sessionFactory" />
    <!--<property name="testSessionFactory" ref="sessionFactory" />-->
    </bean>

    BasicDao extends HibernateDaoSupport.

    And if I comment out the property it complains that a session factory must be set. I have done some other tests and determined that my Dependancy Injection is working correctly and that setSessionFactory is called with an instance of SessionFactory. Any suggestions on why getSessionFactory continues to return null?

    Cheers guys

    Mike

  • #2
    Can you post the code where this happens?

    Regards,
    Andreas

    Comment


    • #3
      Sure thing,

      here is the SessionFactory bean

      <bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">

      <property name="driverClassName">
      <value>org.hsqldb.jdbcDriver</value>
      </property>
      <property name="url">
      <value>jdbc:hsqldb:hsql</value>
      </property>
      <property name="username">
      <value>sa</value>
      </property>
      <property name="password">
      <value></value>
      </property>
      </bean>

      and here is the class


      public class BasicDao extends HibernateDaoSupport {

      /**
      * Obtains a <code>List</code> of all persisted records of type
      * <code>clazz</code>.
      *
      * @param clazz
      * the name class to select from
      *
      * @return an unmodifiable list of all class records
      *
      */
      public List getAll(String clazz) throws ClassNotFoundException {

      return getHibernateTemplate().find("from " + Class.forName(clazz));
      }

      public Object getById(String code, String clazz)
      throws ClassNotFoundException {

      /**********************************
      * Just here, getHibernateTemplate() and getSessionFactory() both return a big fat null!
      ***********************************/
      SessionFactory factory = getSessionFactory();
      List accounts = getHibernateTemplate().findByNamedParam(
      "select a from " + Class.forName(clazz)
      + "as a where a.id=:code", new String[] { code },
      new Object[] { code });

      return accounts.get(0);
      }

      public void save(Object o) {
      getHibernateTemplate().saveOrUpdate(o);
      }

      public void delete(Object o) {
      getHibernateTemplate().delete(o);
      }

      }


      Cheers for your help mate. I have been working on this all day and not getting anywhere!

      Comment


      • #4
        Your configuration is correct so it's likely that you get a different basicDao instance (not managed by Spring - so it doesn't have the properties set).
        Double check your code and walk through the debugger - either somebody sets a null SF on your basicDAO (put some sysout statements in there) or you are using another basicDAO instance.

        Comment


        • #5
          Thanks :-)

          Cant beleive I am that stupid sometimes. DBUnit was gettin its own instance. Well done, much appreciated!

          MC

          Comment

          Working...
          X