Question

I'm working with Spring 3.0.5,Hibernate 3.3 and generic-hibernate-dao. I've configured Hibernate SessionFactory as below:

<bean id="sessionFactory"   class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean?"> 
    <property name="dataSource">
        <ref local="dataSource" /> 
    </property> 
<property name="packagesToScan" value="com.xxx.re.admin.model" /> <property name="hibernateProperties">

        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect 
            </prop> 
            <prop key="hibernate.show_sql">false</prop> 
            <prop key="hibernate.hbm2ddl.auto">validate</prop> 
        </props> 
    </property> 
</bean> 

<!-- Transaction manager for a single Hibernate SessionFactory? (alternative
    to JTA) --> 
<tx:annotation-driven /> 

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager?"> 
    <property name="sessionFactory">
        <ref local="sessionFactory" /> 
    </property> 
</bean> 

I've created a BaseDAOImpl and extended with a domain DAO as below:

public class BaseDAOImpl<T, ID extends Serializable> extends GenericDAOImpl<T, ID> {
    @Autowired @Override public void setSessionFactory(SessionFactory? sessionFactory) {
        super.setSessionFactory(sessionFactory); 
    } 
}

@Repository public class LocaleDAOImpl extends BaseDAOImpl<Locale, Long> implements LocaleDAO {

}

On accessing my spring controller (calling the dao.findAll()) I get the following error:

org.hibernate.HibernateException?: 
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.
Was it helpful?

Solution

Annotate your controller with @Transactional (or maybe some service if it is an intermediate between your controller and DAO). Service is actually a better place, otherwise you need to place <tx:annotation-driven /> in *-servlet.xml MVC configuration file as well.

I have never used this library (I personally use Spring Data JPA from Spring portfolio), but the documentation doesn't say anything about transactions, so I guess it is up to the user to configure them.

UPDATE: looking at the examples they provide seems like I am right:

@Transactional
public class CitizenServiceImpl implements CitizenService {
//...

http://code.google.com/p/hibernate-generic-dao/source/browse/trunk/sample/jpa-hibernate-maven/src/main/java/sample/googlecode/genericdao/service/CitizenServiceImpl.java?r=635

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top