Boa noite, estou tentando criar um projeto utilizando Hibernate e Spring, obtive sucesso criando algumas configurações, inclusive consegui gerar o banco de dados através da inicialização da aplicação utilizando o Spring, porém estou "empacado" na criação dos meus DAO's, utilizo a anotação @Autowired para injetar uma sessionFactory do hibernate criada no contexto do spring na minha classe DaoGenerico, mas ao tentar realizar testes recebo o erro NullPointerException quando a sessionFactory tenta recuperar uma sessao através do método getSession(), abaixo seguem alguns códigos:
Interfaces:
DaoI
public interface DaoI<T> {
public void persistir(T objeto);
public void excluir (T objeto);
public T get(Integer id);
public List<T> listar(int de, int ate);
}
DaoUsuarioI
public interface DaoUsuarioI<T> extends DaoI<T>{
public void addUsuario(Usuario usuario);
public void removerUsuario(Usuario usuario);
public Usuario getUsuario(Integer id);
public void atualizaUsuario(Usuario usuario);
public List<Usuario> getUsuarios();
}
Classes:
DaoGenerico
@Transactional(propagation=Propagation.SUPPORTS)
public abstract class DaoGenerico<T> implements DaoI<T> {
@Autowired
private SessionFactory sessionFactory;
private Session getSession(){return getSessionFactory().getCurrentSession();}
public SessionFactory getSessionFactory() {return sessionFactory;}
public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}
@SuppressWarnings("rawtypes")
protected abstract Class getClazz();
public void persistir(T objeto) {
getSession().persist(objeto);
}
public void excluir(T objeto) {
getSession().delete(objeto);
}
@SuppressWarnings("unchecked")
public T get(Integer id) {
return (T) getSession().get(getClazz(), id);
}
@SuppressWarnings("unchecked")
public List<T> listar(int de, int ate) {
return (List<T>) getSession().createCriteria(getClazz()).setMaxResults(ate).setFirstResult(de).list();
}
@SuppressWarnings("unchecked")
public List<T> listar() {
return (List<T>) getSession().createCriteria(getClazz()).list();
}
DaoUsuario
@Transactional(propagation=Propagation.SUPPORTS)
@Repository("daoUsuario")
public class DaoUsuario extends DaoGenerico<Usuario> implements DaoUsuarioI {
public void addUsuario(Usuario usuario) {
persistir(usuario);
}
public void removerUsuario(Usuario usuario) {
removerUsuario(usuario);
}
public Usuario getUsuario(Integer id) {
return get(id);
}
public void atualizaUsuario(Usuario usuario) {
Session s = super.getSessionFactory().getCurrentSession();
s.update(usuario);
}
public List<Usuario> getUsuarios() {
return listar();
}
@SuppressWarnings("rawtypes")
protected Class getClazz() {
return Usuario.class;
}
}
O meu contexto esta configurado no arquivo spring-data.xml, segue abaixo
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="br.com.springframework.persistencia" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="*******" />
<property name="password" value="********" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springframework"/>
<property name="maxPoolSize" value="30" />
<property name="acquireIncrement" value="1"/>
<property name="maxIdleTime" value="120"/>
<property name="acquireRetryAttempts" value="10"/>
<property name="initialPoolSize" value="1" />
</bean>
<bean id="sessionFactory" name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="br.com.springframework.entidades" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
Estou tentando criar testes com o JUnit para verificar o funcionamento das classes acima, porém sem sucesso... alguem poderia ajudar?
Obrigado...