The Hibernate session session is still null in my DAO for a regular java class

advertisements

I see so many questions on this and have tried numerous permutations of the many different solutions and none have worked.

I have a dao that requires a hibernate sessionfactory to do transactions. In the SpringMVC Context I've seen it working but the dao included in a java class is null. There is no error in catalina.out:

my full applicationContext.xml(because I really think the problem is here somewhere):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation=
   "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Scan classpath for annotations (eg: @Service, @Repository etc)-->

<context:annotation-config/>

<context:component-scan base-package="com.shazam.di.*" />

<!-- JNDI Data Source. this works I can get to it independent of spring-->
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
scope="singleton">
    <property name="jndiName" value="jdbc/dostudentdb"/>
    <property name="resourceRef" value="true"/>
</bean>

<tx:annotation-driven/>

<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
        <value>com.shazam.di.spring.coursemgmt.dao</value>
    </array>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">false</prop>
            <!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!--I've alternated between contructor, properties for getters & setters -->
<!--here, and nothing (just letting it get autowired into the private-->
<!--SessionFactory instance, no effing cigar!!-->
<bean id="studentDAO" class="org.shazam.di.spring.coursemgmt.dao.StudentDAO">
    <!--<constructor-arg type="SessionFactory" value="mySessionFactory"/>-->
    <property name="insertUserProfile" ref="insertUserProfile"/>
</bean>
</beans>

The class for which the DAO but not the sessionFactory can be found:

@Component
public class CheckClassAccess
{
    @Autowired
    private static StudentDAO studentDAO;...

The beginning of the DAO(tried autwiring only getter & setters and a constructor):

@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class StudentDAO {

    @Autowired
    private SessionFactory sessionFactory;
etc...

The WEB XML Spring lines:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:WEB-INF/applicationContext.xml</param-value>
</context-param>
and then a little later...
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

The only other caveat to this is that I'm attempting to get this to work within an opensource java cms called Opencms. But not sure that's relevant as the files that I'm wiring to are vanilla java support class, not controllers or anything(not really looking to do Spring-MVC with it just yet).

In fact all of this works in a Spring MVC servlet-context on a seperate smaller application but I just cannot seem to get these same objects/annotations to register in the applicationContext.


If you are using annotations naming matters. So change your StudentDAO as following:

@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sessionFactory;

Look at this for more explanation.

Alternatively Spring recommends @Resource annotation:

@Resource("mySessionFactory")
private SessionFactory sessionFactory;