简体   繁体   English

配置Spring + Hibernate Transaction Manager

[英]Configure Spring + Hibernate Transaction Manager

I am trying to configure the transaction manager in Spring in order to annotate my service methods with @Transactional . 我试图在Spring中配置事务管理器,以便使用@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 . 不幸的是,我一直在寻找其他解决方案,但是找不到适合我的解决方案。我正在使用Tomcat7。服务器启动时没有错误,但是当我调用服务方法时,它根本无法识别@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 : getByID(String id)是被调用的方法,如果我手动启动一个事务,从数据库中检索数据没有问题,当我用@Transactional注释它时会抛出此错误:

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

applicationContext.xml 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 的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. 请我需要一些建议。我认为这是一个配置问题,并且已经尝试解决了一段时间。

You need to register your DAO as a spring component and tell spring where to find it by defining <context:component-scan > . 您需要将DAO注册为spring组件,并通过定义<context:component-scan >告诉spring在哪里找到它。 Check out this example . 看看这个例子

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 . 如果要使用spring事务,则必须通过xml bean configuration或使用@Component,@Service, @Repository类的annotations将类声明为bean

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 如果要使用批注,则需要确保xml配置中的component scan元素可以扫描您的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM