PDA

View Full Version : IoC not working with Hibernate


mateamargo
Apr 25th, 2007, 11:35 AM
I have a class that holds an object that extends HibernateDAOSupport class.
I need to set this DAO object with Spring, and this is how I did it:

<bean id="SomeClass" class="com.foo.SomeClass">
<property name="daoObject" ref="myDaoObject" />
</bean>

<bean id="myDaoObject" class="com.foo.dao.DAOOject">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>WEB-INF/mappings</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.PostgreSQL Dialect
hibernate.current_session_context_class=thread
hibernate.cache.provider_class=org.hibernate.cache .EhCacheProvider
hibernate.show_sql=false
hibernate.hbm2ddl.auto=update
hibernate.query.factory_class=org.hibernate.hql.cl assic.ClassicQueryTranslatorFactory
</value>
</property>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
<property name="username"><value>user</value></property>
<property name="password"><value>pass</value></property>
<property name="url"><value>jdbc:postgresql://server:5432/database</value></property>
</bean>


I finally run mvn jetty:run to get the Jetty server load. But when I go to the page that uses com.foo.SomeClass, I get a NullPointerException (because is trying to execute a method from this DAO object).

I don't know if I missed something.

karldmoore
Apr 25th, 2007, 01:00 PM
Are you asking Spring for your beans, or are you creating them with the new operator? If that's the case then Spring know about them and hence the dependencies won't have been injected.

mateamargo
Apr 25th, 2007, 01:26 PM
I'm not creating with the new operator.
I guess the problem is that I'm using Tapestry.

karldmoore
Apr 25th, 2007, 01:49 PM
If you are injecting the properties and it's just not there when I'd guess it must be whatever code is getting hold of the bean. I can't comment on the specifics as I haven't really used Tapestry.