zodiac
zodiac

Reputation: 353

Transactional annotation doesn't work

I use spring 3.2.8, hibernate 4. I have an error "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: createCriteria is not valid without active transaction"

web.xml:

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>journeys</display-name>

 <servlet>
    <servlet-name>journeys</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>journeys</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/journeys-servlet.xml</param-value>
  </context-param>

  <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
  </listener>

  <jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
  </jsp-config>
</web-app>

journeys-servlet.xml:

<beans xmlns="...">
    <context:annotation-config />
    <context:component-scan base-package="journeys.*" />

    <bean id="viewResolver"
        ...
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

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

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

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

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
</beans>

class/hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
      <!-- Mapping files -->
        <mapping class="journeys.entity.Company"/>
        ....
    </session-factory>
</hibernate-configuration>

CompanyDAO:

package journeys.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import journeys.utils.Pair;
import journeys.entity.Company;
import journeys.entity.Journey;
import journeys.entity.JourneyDeparture;
import journeys.entity.Order;

@Component
public class CompanyDAO extends AbstractDAO<Company> {
    @Autowired
    private SessionFactory sessionFactory;

    public CompanyDAO() {
        super(Company.class);
    }

    @Transactional
    @SuppressWarnings("unchecked")
    public List<Company> f() {
        return (List<Company>)sessionFactory.getCurrentSession().createCriteria(Company.class).list();
    }
}

CompanyController:

package journeys.controller;

import journeys.dao.CompanyDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/company")
public class CompanyController {    
    @Autowired
    CompanyDAO companydao;

    @RequestMapping(value = {"", "/", "list"}, method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView model = new ModelAndView("Company/list");
        model.addObject("companies", companydao.f());

        return model;
    }
}

Upvotes: 1

Views: 1213

Answers (2)

geoand
geoand

Reputation: 64079

Do the following and the problem goes away:

In your hibernate.cfg.xml add the following property:

<property name="hibernate.current_session_context_class"> org.springframework.orm.hibernate4.SpringSessionContext
</property>

Also remove the line <property name="current_session_context_class">thread</property> that you currently have in your code

Upvotes: 7

yotsov
yotsov

Reputation: 775

A common reason for this that I have seen is the lack of "engine" to apply the effect of @Transactional. Namely, in your pom you need to have a runtime dependency either on cglib or jaspect. Seeing how your tx-manager is defined, it seems you are likely to need cglib.

Edit:

Also, to enable cglib, use:

<tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/>

instead of just

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

Upvotes: 0

Related Questions