【问题标题】:Configure Spring + Hibernate Transaction Manager配置 Spring + Hibernate 事务管理器
【发布时间】:2017-07-30 02:52:23
【问题描述】:

我正在尝试在 Spring 中配置事务管理器,以便使用 @Transactional 注释我的服务方法。不幸的是,我一直在寻找其他解决方案,但找不到适合我的解决方案。我使用的是 Tomcat 7。服务器启动时没有错误,但是当我调用服务方法时,它根本无法识别@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) 是被调用的方法。如果我手动启动事务,从数据库中检索数据没有问题。当我用@Transactional 对其进行注释时,它会抛出此错误:

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>

我需要一些建议。我认为这是一个配置问题,我已经尝试解决了一段时间。

【问题讨论】:

    标签: java spring hibernate configuration spring-transactions


    【解决方案1】:

    你需要将你的 DAO 注册为一个 spring 组件,并通过定义 &lt;context:component-scan &gt; 告诉 spring 在哪里可以找到它。查看this example

    【讨论】:

    • 这不是问题,但您的链接帮助我解决了问题。我总是在我的代码中初始化一个新的 SessionFactory 而不是 @Autowired。会话工厂已经在 xml 中声明了。
    【解决方案2】:

    如果您想使用 spring 事务,则必须通过 xml bean configuration 或使用 annotations (如 @Component,@Service, @Repository)将类声明为 bean

    如果你想使用注解,那么你需要确保你的 xml 配置中的 component scan 元素可以扫描你的类

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2012-10-16
      • 2010-10-18
      • 1970-01-01
      • 2014-02-22
      相关资源
      最近更新 更多