Reputation: 357
I am trying to write a simple spring-hibernate command line application with transaction management support, but I am constantly getting "org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"
my spring config file (applicationContext.xml)
<!-- Context Provider -->
<bean class="com.rps.util.ContextUtil" />
<!-- The transactional service objects -->
<bean id="service" class="com.rps.service.DefaultService" p:parentDao-ref="parentDao" p:childDao-ref="childDao" />
<!--the transactional advice-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--this transactional advice runs for any service method execution-->
<aop:config>
<aop:advisor pointcut="execution(* com.rps.service..*.*(..))" advice-ref="txAdvice" />
</aop:config>
<!-- The PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- The DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/OsivTest" p:username="XXX" p:password="YYY" />
<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
<property name="mappingResources">
<list>
<value>com/rps/domain/Parent.hbm.xml</value>
<value>com/rps/domain/Child.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Data Access Objects -->
<bean id="parentDao" class="com.rps.data.hbm.HibernateParentDao" p:sessionFactory-ref="sessionFactory" />
<bean id="childDao" class="com.rps.data.hbm.HibernateChildDao" p:sessionFactory-ref="sessionFactory" />
DAO Code (Session Factory object set by IoC container as configured in context file above)
public List<T> findAll() {
return sessionFactory.getCurrentSession().createCriteria(getPersistentClass()).list();
}
DefaultService Class: (parentDao injected from IoC container)
public class DefaultService implements Service {
@Override
public List<Parent> getAllParent() {
return parentDao.findAll();
}
//Other methods
}
Main method code:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Service s = (Service) context.getBean("service");
s.getAllParent();
}
I am using hibernate version 3.1.3 and spring 3. How do we configure one session per thread? I believe the issues is that no session is being associated with my execution thread. I was assuming that transaction manager will open a session if not already found, but it does not do so.
Upvotes: 1
Views: 438
Reputation: 298898
Quote 1:
I am using hibernate version 3.1.3 and spring 3
Quote 2:
Note
As of Spring 3.0, Spring requires Hibernate 3.2 or later.
(Source)
BTW, the current stable Hibernate version is 3.6.7
Upvotes: 0