在进行Spring和hibernate的整合时,利用Spring的声明式事务管理,得到一个异常:no transaction is in progress
在网上查阅相关资料,发现有的需要加注解@Transactional,有的是因为加了两个实体管理类,但是都无法解决我的问题。
以下是相关代码:
bean.xml
<bean id="u" class="com.Spring.DAO.UserDAOMySQLimplement">
<property name="sessionfactor" ref="SessionFactory"/>
</bean>
<bean id="LogDAO" class="com.Spring.DAO.LogDAOMySQLimplement">
<property name="sessionfactor" ref="SessionFactory"/>
</bean>
<bean id="Service" class="com.Spring.Service.UserService">
<property name="dao" ref="u"/>
<property name="logdao" ref="LogDAO"/>
</bean>
<!-- 定义一个数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/Spring"/>
<property name="username" value="root"/>
<property name="password" value="tiger"/>
</bean>
<bean id="SessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.Spring.Model.User</value>
<value>com.Spring.Model.Log</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
</value>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
Service层
public class UserService {
private UserDAO dao=null;
private LogDAO logdao;
public LogDAO getLogdao() {
return logdao;
}
public void setLogdao(LogDAO logdao) {
this.logdao = logdao;
}
@Transactional("txManager")
public void add(User u){
dao.add(u);
Log log=new Log();
log.setMsg("日志:成功存入用户");
logdao.add(log);
}
public UserDAO getDao() {
return dao;
}
public void setDao(UserDAO dao) {
this.dao = dao;
}
}
UserDAO层
public class UserDAOMySQLimplement implements UserDAO {
SessionFactory sessionfactor; //定义一个sessionfactor,使用自动注入来初始化。
public SessionFactory getSessionfactor() {
return sessionfactor;
}
public void setSessionfactor(SessionFactory sessionfactor) {
this.sessionfactor = sessionfactor;
}
//往数据库保存一条记录
public void add(User u) {
Session session=sessionfactor.getCurrentSession();
session.save(u);
}
LogDAO层
public class LogDAOMySQLimplement implements LogDAO {
SessionFactory sessionfactor; //定义一个sessionfactor,使用自动注入来初始化。
public SessionFactory getSessionfactor() {
return sessionfactor;
}
public void setSessionfactor(SessionFactory sessionfactor) {
this.sessionfactor = sessionfactor;
}
//往数据库保存一条记录
@Override
public void add(Log log) {
Session session=sessionfactor.getCurrentSession();
session.save(log);
}
}
测试代码
public void Test_add(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
User u=new User();
u.setName("小明");
UserService us=(UserService) context.getBean("Service");
System.out.println(us.getClass());
us.add(u);
}
所用版本Spring4.3.4,hibernate5.2.5
想要完成的业务逻辑是往数据库插入一条User记录,并且往日志表插入一条日志。
交易管理器
雷雷