Bom dia, estou tentando configurar uma SessionFactory do Spring com o hibernate, porém ao subir a aplicação O Logger do Hibernate escreve :
INFO: HHH000041: Configured SessionFactory: null
E quando eu tendo dar um sessionFactory.getCurrentSession(), ele me retorna null sempre.
Seguem as configurações :
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="model.Pessoa" />
<mapping class="model.Usuario" />
<mapping class="model.Materia" />
<mapping class="model.Prova" />
<mapping class="model.Questao" />
<mapping class="model.Alternativa" />
<mapping class="model.ProvaRealizada" />
<mapping class="model.Telefone" />
<mapping class="model.Endereco" />
<mapping class="model.Estado" />
<mapping class="model.Pais" />
</session-factory>
</hibernate-configuration>
springConfig.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config/>
<context:component-scan base-package="." />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="org.postgresql.Driver"
p:url="jdbc:postgresql://localhost:5432/yttria" p:username="postgres"
p:password="projetos" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Classe com a SessionFactory
package dao;
import model.Usuario;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class UsuarioDao extends GenericDao<Usuario>{
private SessionFactory sessionFactory;
public UsuarioDao(){
super(Usuario.class);
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
super.setSessionFactory(sessionFactory);
}
}
GenericDao que todos os Daos Extendem:
package dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
public class GenericDao<T extends Serializable> implements IDao<T>{
private Class<T> typeClass;
private SessionFactory sessionFactory;
public GenericDao(Class<T> type) {
typeClass = type;
}
private List<T> listAll() {
List<T> result = null;
Query query = getSession().createQuery("from " + typeClass.getName());
result = query.list();
return result;
}
@Override
public List<T> list(Criterion[] criterions) {
Criteria criteria = getSession().createCriteria(typeClass);
List<T> result = null;
if(criterions != null){
loadCriterions(criterions, criteria);
}
result = criteria.list();
return result;
}
@Override
public T save(T obj) {
return (T) getSession().save(obj);
}
@Override
public void update(Criterion[] criterions, T obj) {
if(obj == null){
Criteria criteria = getSession().createCriteria(typeClass);
loadCriterions(criterions, criteria);
obj = (T) criteria.uniqueResult();
}
getSession().update(obj);
}
@Override
public void delete(Criterion[] criterions, T objDel) {
if(objDel == null){
Criteria criteria = getSession().createCriteria(typeClass);
loadCriterions(criterions, criteria);
objDel = (T) criteria.uniqueResult();
}
getSession().delete(objDel);
}
@Override
public T load(Criterion[] criterions) {
Criteria criteria = getSession().createCriteria(typeClass);
T result = null;
if (criterions != null){
loadCriterions(criterions, criteria);
result = (T) criteria.uniqueResult();
}
return result;
}
private void loadCriterions(Criterion[] criterions, Criteria criteria){
for (Criterion ct : criterions) {
criteria.add(ct);
}
}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
private Session getSession(){
return sessionFactory.getCurrentSession();
}
}
