Getting Factory null in spring sessionfactory with configured resource

Asked

Viewed 154 times

5

The name of the class containing the SessionFactory is DataProvider and has the following implementation:

@Resource(name="sessionFactory")
protected SessionFactory factory;

protected Class<E> entity;
protected String tableName;

public DataProvider(Class e) {
   this.entity = e;
   this.tableName = entity.getAnnotation(Table.class).name();
}

@Transactional(readOnly = true)
public List<E> getAll() {
    Session s = factory.getCurrentSession(); // A exceção acontece aqui.
    return s.createQuery("FROM " + tableName ).list();
}

The bean with the configuration of the SessionFactory is:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="models"/>
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

I’m getting NullPointerException in the commented line. The question is: am I doing something wrong or still missing some configuration?

  • This XML is the persistence.xml or is another?

  • The class this code is in is a Bean managed by Spring?

  • @Ciganomorrisonmendez XML is from Spring.

  • You can update your reply to have the persistence.xml + the complete Bean code + the part of XML that configures the Bean injections?

  • @Ciganomorrisonmendez is the applicationContext.xml

  • And the package your classes are in is just models even?

  • @Miguelcartagena Yes!

  • What version of Spring are you using?

Show 3 more comments

1 answer

2

For Spring to resolve dependencies using Annotations, you need to add an extra configuration to your applicationContext.xml:

<?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:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>

<context:component-scan base-package="br.com.meupacote." />

<!-- seus outros beans -->


</beans>

The configuration <context:annotation-config/> tells Spring that it must resolve dependencies indicated by Annotations (either @Resouce, @Autowired or @Inject), the configuration <context:component-scan base-package="br.com.meupacote" /> tells Spring to look for these dependencies in the classes that belong to the package br.com.meupacote.* .

Also, for Spring to resolve dependencies in its class DataProvider this class should belong to the Spring context. An easy way to set this up is to write it down with @Component. Example:

@Component
public class DataProvider{
 // ....
} 

Documentation of version 3.0.0: http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s11.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.