Reputation: 1
I have an application in JSF, spring, Hibernate and Spring security, the interaction between the hibernate layer and the database was going good, once the spring security layer the queries don't render anything.
Any solution ?
I'm creating my SessionFactory from hibernate config file
The Filters in web.xml: ....................
!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
.................
The hibernate session factory creation class :
...........................
package Util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
Configuration configuration = new AnnotationConfiguration();;
configuration.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(sr);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
@Transactional
public class stufImp implements stufDAO , Serializable {
private SessionFactory sf = HibernateUtil.getSessionFactory();
Session session ;
public stufImp() {
}
@Override
public List<Stuf> getAllstufs() {
session=sf.getCurrentSession();
session.beginTransaction();
List<Stuf> stufs= session.createQuery("from Stuf").list();
session.getTransaction().commit();
System.out.println("getting All Stufs");
return stufs;
}
....................
Upvotes: 0
Views: 576
Reputation: 125202
For starters, first delete the HibernateUtil you use to configure hibernate, next fix your dao. You should never store the Session
in an instance variable, at least not when your dao is a singleton. (imagine what would happen if 2 concurrent requests come in, what happens to the session!).
For configuring the hibernate SessionFactory
use the LocalSessionFactoryBean
from spring. (More information in the reference guide and javadoc).
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
</bean>
And you also need the transaction manager and enable annotation-driven transactions.
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven /> <!-- By default references 'transactionManager' -->
Modify your dao (see reference guide).
@Transactional
public class stufImp implements stufDAO , Serializable {
@Autowired
private SessionFactory sf;
@Override
public List<Stuf> getAllstufs() {
return sf.getCurrentSession().createQuery("from Stuf").list();
}
}
Links
Upvotes: 1