hi all,
i try to use the spring framework to handle hibernate's session stuff.
i use hibernate 2.1.2, postgresql 7.2.1 beta (windows port), and tomcat 4.1.24.
when tomcat start and initialize my webapp, all is fine:
=> no exceptions are thrown.
but, when i want to use hibernate, with this first line:
Code:
session = SessionFactoryUtils.getSession(getSessionFactory(),false);
an illegalstateexception is thrown.
by the way, the reason is the sessionfactory is null.
i have configured my web.xml with the right listener to start spring:
Code:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
does spring initialize itself the sessionFactory?
what is the mistake?
my applicationContext.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- DATASOURCES DEFINITIONS -->
<!-- forum Datasource Factory definition -->
<bean id="forumDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/postgres</value>
</property>
</bean>
<!-- registration Datasource Factory definition -->
<bean id="regDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/postgres</value>
</property>
</bean>
<!-- SESSION FACTORIES DEFINITIONS -->
<!-- forum Session Factory definition -->
<bean id="forumSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>net/sf/jboard/model/forum/dto/ForumCategory.hbm.xml</value>
<value>net/sf/jboard/model/forum/dto/ForumItem.hbm.xml</value>
<value>net/sf/jboard/model/forum/dto/ForumThread.hbm.xml</value>
<value>net/sf/jboard/model/forum/dto/ForumMessage.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
</props>
</property>
<property name="dataSource">
<ref bean="forumDataSource"/>
</property>
</bean>
<!-- registration Session Factory definition -->
<bean id="regSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>net/sf/jboard/model/reg/dto/User.hbm.xml</value>
<value>net/sf/jboard/model/reg/dto/Role.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
</props>
</property>
<property name="dataSource">
<ref bean="regDataSource"/>
</property>
</bean>
<!-- AOP INTERCEPTORS -->
<!-- forum interceptor -->
<bean id="myForumHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="forumSessionFactory"/>
</property>
</bean>
<bean id="myForumDaoTarget" class="net.sf.jboard.model.forum.dao.ForumHibernateDAO">
<property name="sessionFactory">
<ref bean="forumSessionFactory"/>
</property>
</bean>
<bean id="myForumDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>net.sf.jboard.model.forum.dao.ForumDAOInterface</value>
</property>
<property name="interceptorNames">
<list>
<value>myForumHibernateInterceptor</value>
<value>myForumDaoTarget</value>
</list>
</property>
</bean>
<!-- registration interceptor -->
<bean id="myRegHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="regSessionFactory"/>
</property>
</bean>
<bean id="myRegDaoTarget" class="net.sf.jboard.model.reg.dao.RegHibernateDAO">
<property name="sessionFactory">
<ref bean="regSessionFactory"/>
</property>
</bean>
<bean id="myRegDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>net.sf.jboard.model.reg.dao.RegDAOInterface</value>
</property>
<property name="interceptorNames">
<list>
<value>myRegHibernateInterceptor</value>
<value>myRegDaoTarget</value>
</list>
</property>
</bean>
</beans>
and my DAO method called:
Code:
/**
* checks if a user with the same username already exists.
* @param User
* @return result. true if a user with the same name exists, false otherwise.
*/
public List userAlreadyExists(User user) throws DAOException {
try{
session = SessionFactoryUtils.getSession(getSessionFactory(),false);
}catch(DataAccessResourceFailureException darfe){
logger.error("erreur spring = "+darfe.getMessage());
throw new DAOException(darfe.getMessage());
}catch(IllegalStateException ise){
logger.error("erreur spring = "+ise.getMessage());
throw new DAOException(ise.getMessage());
}
int rowNumber = 0;
List list;
try {
Query q = session.createQuery("select user.id from net.sf.jboard.model.reg.dto.User as user " +
"where user.username=:username and user.password=:password ");
q.setParameter("username",user.getUsername());
q.setParameter("password",user.getPassword());
list = q.list();
} catch (HibernateException e) {
throw new DAOException(e.getMessage());
}
return list;
}
you can note that i have configured two DAO which request to the same datasource (one of them will refers to another datasource later).
if someone has got an experience of Spring..........
sincerly yours,
charles.