Announcement Announcement Module
Collapse
No announcement yet.
getHibernateTemplate is null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getHibernateTemplate is null

    Hi All,

    I am playing with Spring for the first time and I have the following code when trying to persist a object with hibernate.

    public class ClientDAOHibernate extends HibernateDaoSupport implements ClientDAO
    {

    public void saveclient(clientVo client)

    System.out.println("entered the saveClientDetail" + client.toString());
    HibernateTemplate hibTemp = getHibernateTemplate();

    hibTemp.saveOrUpdate(client);
    System.out.println("exiting the saveClientDetail");

    The problem I have is that the HibernateTemplate is null.

    my Application context is as follows:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>com.mysql.jdbc.Drive r</value></property>
    <property name="url"><value>jdbc:mysql://localhost:3306/medinatravels</value></property>
    <property name="username"><value>root</value></property>
    <property name="password"><value></value></property>
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>com/medina/transferobjects/HajjVO.hbm.xml</value>
    <value>com/medina/transferobjects/ClientVO.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>


    <bean id="ClientDAO" class="com.medina.db.dao.hibernate.ClientDAOHibern ate">
    <property name="sessionFactory"><reflocal="sessionFactory"/></property>
    </bean>


    I can persist the client if I just use hibernate on its own but if i try to integrate this with Spring i get the following error.

    2005-05-17 15:50:23 StandardWrapperValve[TestDB]: Servlet.service() for servlet TestDB threw exception
    java.lang.NullPointerException
    at com.medina.db.dao.hibernate.ClientDAOHibernate.sav eClient(ClientDAOHibernate.java:35)
    at com.medina.web.TestDB.service(TestDB.java:56)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at

    All help is appreciated.

  • #2
    Some further info which may be of help:

    This is how I instantiate the ClientDAOHibernate from a servlet service method.

    ClientDAO clientDAO = new ClientDAOHibernate();
    clientDAO.saveClient(client);

    So the applicationcontext.xml is loaded from the web.xml from the web-inf directiry

    Comment


    • #3
      Some further info which may be of help:

      This is how I instantiate the ClientDAOHibernate from a servlet service method.

      ClientDAO clientDAO = new ClientDAOHibernate();
      clientDAO.saveClient(client);

      So the applicationcontext.xml is loaded from the web.xml from the web-inf directiry

      Comment


      • #4
        Fez, you are suppose to get the beans from the Spring context not create them yourself.
        Spring doesn't change any Java semantics and it's dependecy injection or inversion of control is applied inside it's container - if you want these features you need to get the beans from Spring.
        I suggest you read the reference documentation and look at the samples provided inside the distribution. You'll find some good templates along with the configuration boiler plate which (especially on the web tier) makes everything very transparent.

        Comment

        Working...
        X