Results 1 to 6 of 6

Thread: help with simple CRUD/Hibernate config

  1. #1
    Join Date
    Apr 2006
    Posts
    3

    Default 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:
    getHibernateTemplate().save(pojoObj)
    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">
    <value>java:/comp/env/jdbc/amadeus</value>
    </property>
    </bean>

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

    bean config for sessionFactory:
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    <list>
    ...hbm xml files...
    </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>

    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"
    auth="Container"
    name="jdbc/amadeus"
    factory="org.apache.commons.dbcp.BasicDataSourceFa ctory"
    initialSizse="30"
    maxActive="100"
    maxIdle="30"
    maxWait="10000"
    username="root"
    password="root"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/amadeus?autoReconnect=true"
    removeAbandoned="true"
    removeAbandonedTimeout="60"
    logAbandoned="true"
    />

    </Context>

    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. #2
    Join Date
    Mar 2005
    Location
    San Francisco, CA
    Posts
    114

    Default

    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. #3
    Join Date
    Apr 2006
    Posts
    3

    Default

    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. #4
    Join Date
    Mar 2005
    Location
    San Francisco, CA
    Posts
    114

    Default

    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. #5
    Join Date
    Apr 2006
    Posts
    3

    Default

    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. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

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