交易注释不起作用

时间:2014-04-26 13:53:31

标签: java spring hibernate

我使用spring 3.2.8,hibernate 4.我有一个错误“org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.hibernate.HibernateException:如果没有活动事务,createCriteria无效”< / p>

的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>

行程-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>

类/ 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;
    }
}

2 个答案:

答案 0 :(得分:7)

执行以下操作并解决问题:

在你的hibernate.cfg.xml中添加以下属性:

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

同时删除您代码中当前拥有的行<property name="current_session_context_class">thread</property>

答案 1 :(得分:0)

我看到的一个常见原因是缺少&#34;引擎&#34;应用@Transactional的效果。也就是说,在你的pom中你需要对cglib或jaspect有一个运行时依赖。看看你的tx-manager是如何定义的,似乎你可能需要cglib。

编辑:

另外,要启用cglib,请使用:

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

而不仅仅是

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