Moroianu Alexandru
Moroianu Alexandru

Reputation: 167

Configure Spring + Hibernate Transaction Manager

I am trying to configure the transaction manager in Spring in order to annotate my service methods with @Transactional. Unfortunately,I have been looking on others solutions ,but I couldn't find one for me.I am using Tomcat 7.The server starts with no errors but when I call the service method it simply does not recognize @Transacional.

public class GenericDaoImplementation<T> 
{
    private Class<T> entityClass;

    public GenericDaoImplementation(Class<T> entityClass) 
    {
        this.entityClass = entityClass;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public T getByID(String id)
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();    
        T obj = (T) session.get(entityClass, id);

        return obj;
    }            
}

getByID(String id) is the called method.It has no problem retrieving the data from database if I start a transaction manually.When I annotate it with @Transactional it throws this error :

org.hibernate.HibernateException: get is not valid without active transaction

applicationContext.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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <context:component-scan base-package="dk.accunu" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
         <property name="packagesToScan">
            <list>
                <value>dk.accunu.model</value>
            </list>
        </property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

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


</beans>

hibernate.cfg.xml

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
 <property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Display all generated SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<mapping class="dk.accunu.model.entities.User" />

</session-factory>

Please I need some advices.I think it is a configuration problem and I have been trying to solve it for some time already.

Upvotes: 1

Views: 4972

Answers (2)

Learner
Learner

Reputation: 21405

If you want to use spring transaction then the class must be declared as a bean either through xml bean configuration or using annotations like @Component,@Service, @Repository.

If you want to use annotations then you need to make sure that the component scan element in your xml configuration can scan your class

Upvotes: 1

veljkost
veljkost

Reputation: 1932

You need to register your DAO as a spring component and tell spring where to find it by defining <context:component-scan >. Check out this example.

Upvotes: 1

Related Questions