gpt4 book ai didi

java - 配置 Spring + Hibernate 事务管理器

转载 作者:行者123 更新时间:2023-11-30 02:37:28 25 4
gpt4 key购买 nike

我正在尝试在 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>

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

最佳答案

您需要将您的 DAO 注册为 spring 组件,并通过定义 <context:component-scan > 告诉 spring 在哪里可以找到它。查看this example .

关于java - 配置 Spring + Hibernate 事务管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42689948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com