Announcement Announcement Module
No announcement yet.
help with simple CRUD/Hibernate config Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with simple CRUD/Hibernate config

    I'm just trying to test simple CRUD using Spring,Hibernate,Tomcat,MySQL - without any success so far.

    Classes that extend HibernateDAOSupport have NPEs on calls such as:
    and this.getSesssionFactory is always null.

    On app/tomcat start-up, the datasource (through JNDI) is being found as far as I can tell:
    INFO: RDBMS: MySQL, version: 5.0.18-nt
    Apr 9, 2006 3:43:11 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.12

    in my applicationContext.xml:
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">

    bean config for class extending HibernateDAOSupport:
    <bean name="schoolAdminMgr"
    class="com.overture.amadeus.service.HibSchoolAdmin Mgr">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>

    bean config for sessionFactory:
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    ...hbm xml files...
    <property name="hibernateProperties">
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
    <prop key="hibernate.show_sql">true</prop>

    I'm creating a context.xml to be included in META-INF under the web-app in Tomcat. It looks like this:
    <Context reloadable="true" path="amadeus" docBase="amadeus">

    <Resource type="javax.sql.DataSource"
    factory="org.apache.commons.dbcp.BasicDataSourceFa ctory"


    mysql driver, commons-dbcp jars copied to tomcat's common/lib folder.

    The only symptom I'm seeing is the NPE and null sessionFactory in the running web-app.

    I don't know where to focus my attention now on troubleshooting this, any suggestions are much appreciated. I suspect it's probably still something with Tomcat and the datasource...but have exhausted all ideas to try.

  • #2
    From those INFO messages and the fact that the application is starting up completely, it sounds like the SessionFactory is getting created and everything is getting wired up correctly.

    Are you using the schoolAdminMgr bean that was configured by Spring or are you creating a new instance of the bean yourself?


    • #3
      thanks for the reply.

      schoolAdminMgr configured by Spring is what I want.

      What is the best approach to retrieving it?

      Is something like this necessary to properly retrieve a Spring configured bean/class extending HibernateDaoSupport?

      ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContex t(servletContext);
      MyBean myBean = (MyBean)appContext.getBean("myBean");

      Examples I'm finding seem to gloss over the step of a relying class (e.g action or controller class in a web app) calling the DAO layer.


      • #4
        Right. The code you have there is the correct way to get a reference to the Spring-configured bean.

        A good next step would be to have Spring configure your object that calls the DAO. Then Spring can wire up the reference to the DAO for you. That is what you are seeing in the examples and is the "best" way to do it.


        • #5
          So, within an action class (using WebWork) I have:

          XmlWebApplicationContext appContext =
          (XmlWebApplicationContext)WebApplicationContextUti ls.getWebApplicationContext(ServletActionContext.g etServletContext());

          appContext.refresh(); //won't work without this refresh call

          //pojoTestMgr is id of bean in applicationContext.xml
          //it's a dao class that extends HibernateDaoSupport
          PojoTestMgr mgr = (PojoTestMgr)appContext.getBean("pojoTestMgr");

          Calling PojoTestMgr's methods (containing getHibernateTemplate() calls) now work.

          But clearly this is ugly...
          There needs to be a facade/manager layer providing access to the DAO layer, to then be called by web action/controller classes.

          I guess I need to figure out how to declare those relationships in the applicationContext config...


          • #6
            Note that you can setup webwork so that even the actions are managed by Spring - this way all the injection of control is taken care off by Spring and your code will be Spring free.
            You can find more information on the WebWork forums.