Java 配置Spring+;休眠事务管理器
我试图在Spring中配置事务管理器,以便用Java 配置Spring+;休眠事务管理器,java,spring,hibernate,configuration,spring-transactions,Java,Spring,Hibernate,Configuration,Spring Transactions,我试图在Spring中配置事务管理器,以便用@Transactional注释我的服务方法。不幸的是,我一直在寻找其他解决方案,但我找不到适合我的解决方案。我正在使用Tomcat 7。服务器启动时没有错误,但当我调用服务方法时,它根本无法识别@Transacional public class GenericDaoImplementation<T> { private Class<T> entityClass; public GenericDaoImpl
@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;
}
}
applicationContext.xml
dk.accunu.model
hibernate.cfg.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
根
1.
org.hibernate.dialogue.mysqldialogue
线
org.hibernate.cache.NoCacheProvider
真的
更新
请告诉我一些建议。我认为这是一个配置问题,我已经尝试解决了一段时间。您需要将DAO注册为spring组件,并通过定义
告诉spring在哪里可以找到它。签出。如果要使用spring事务,则必须通过XMLBean配置
或使用注释
如@Component、@Service、@Repository
将该类声明为bean
如果要使用注释,则需要确保xml配置中的
组件扫描
元素可以扫描类,这不是问题所在,但您的链接帮助我解决了问题。我总是在我的代码中初始化一个新的SessionFactory,而不是@Autowired
。会话工厂已经用xml声明了。
org.hibernate.HibernateException: get is not valid without active transaction
<?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>
<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>