刚入门SpringMVC 有个地方不明白

我纯洁全身都纯洁 2014-10-24 10:48:39

package com.sxt.dao;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sxt.po.User;

@Component
public class UserDao {
@Resource
private HibernateTemplate hibernateTemplate;

public void add(User u){
System.out.println("UserDao.add()");
hibernateTemplate.save(u);
}

public void delete(User u){
System.out.println("UserDao.delete()");
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery("delete from user where uname=?");
query.setString(0, u.getUname());
query.executeUpdate();
}

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

}


我用的是注解的方式
执行add的时候可以通过。

执行delete的时候我就是想自己写个hql 然后就报错了


exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:583)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)




不知道是什么情况 好像是Session 的问题
这个怎么办?
...全文
277 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kky2010_110 2014-11-04
  • 打赏
  • 举报
回复
<aop:config> <aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> </aop:config> 这是个拦截器,在执行businessService这个bean中的类方法的时候,对该bean的方法加上事务。 execution(public * com.sxt.service.*.*(..))这个是匹配字符串,是指给com.sxt.service的包以及子包中的bean加上事务 com.sxt.dao,给该包下的bean加上事务。 如果你是com.sxt.dao这个的话,在@Component public class UserService { @Resource private UserDao userDao; public void add(String uname){ System.out.println("UserService.add()"); User u = new User(); u.setUname(uname); userDao.add(u); userDao.delete(u); }则添加和删除不在一个事务里面
Mr_JieLQ 2014-10-25
  • 打赏
  • 举报
回复
引用 6 楼 hjgzj 的回复:
[quote=引用 4 楼 sc6231565 的回复:] 一种是没有配置事务 还有一种 是自己当初刚学的时候犯得错误 就是 srpingmvc 把Service层的类 也给扫描了,然后注册到容器 但是 在srping.xml里面配置的事务,是没法用到springmvc扫描的bena里,也会报这种错误,为自己的不熟悉,付出了一下午的debug
引用 5 楼 rui888 的回复:
解决了就好 。哈哈
我还是有问题

<aop:config> 
    <aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 
是做什么用的? 为什么com.sxt.service.*.* 我改成com.sxt.dao.*.* 或改成com.*.*.*.* 程序一样正常?[/quote] 这个很简单,com.sxt.service.*.*可以操作多个表,com.sxt.dao.*.*一般操作一张表,例如汇款,如果配置后面那种,收款方账户不存在,但钱已经扣除了,那钱就回不来了,因为不会回滚操作成功的那张表
tony4geek 2014-10-24
  • 打赏
  • 举报
回复
解决了就好 。哈哈
Magical茏 2014-10-24
  • 打赏
  • 举报
回复
一种是没有配置事务 还有一种 是自己当初刚学的时候犯得错误 就是 srpingmvc 把Service层的类 也给扫描了,然后注册到容器 但是 在srping.xml里面配置的事务,是没法用到springmvc扫描的bena里,也会报这种错误,为自己的不熟悉,付出了一下午的debug
  • 打赏
  • 举报
回复
已解决

<!-- 配置事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<aop:config> 
	<aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 
	<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 

的<aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 配错了。
  • 打赏
  • 举报
回复
引用 1 楼 kky2010_110 的回复:
没有配置事务,你这里没错,dao没错,是在引用dao的地方错了,在上一层方法上加上注解事务就对了

package com.sxt.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.sxt.dao.UserDao;
import com.sxt.po.User;

@Component
public class UserService {
	@Resource
	private UserDao userDao;
	
	public void add(String uname){
		System.out.println("UserService.add()");
		User u = new User();
		u.setUname(uname);
		userDao.add(u);
	}
	
	public void delete(String uname){
		System.out.println("UserService.delete()");
		User u = new User();
		u.setUname(uname);
		userDao.delete(u);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
}
这里吗?
kky2010_110 2014-10-24
  • 打赏
  • 举报
回复
没有配置事务,你这里没错,dao没错,是在引用dao的地方错了,在上一层方法上加上注解事务就对了
Magical茏 2014-10-24
  • 打赏
  • 举报
回复

<aop:config> 
    <aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 
是做什么用的? 答:是spring提供的一套AOP模板,用来注入事务 为什么com.sxt.service.*.* 我改成com.sxt.dao.*.* 或改成com.*.*.*.* 程序一样正常? 答:com.sxt.service.*.* 是将事务注入到Service 的方法里,项目里一般这样配置,因为一个业务逻辑往往需要调用多个dao,为了保证数据的一致性,必须在一个事务里完成 com.sxt.dao.*.** 是将事务注入到dao 的方法里, 如果service调用多个dao,不在同一个事务,是不符合业务逻辑的 com.*.*.*.* 注入到com下
  • 打赏
  • 举报
回复
引用 4 楼 sc6231565 的回复:
一种是没有配置事务 还有一种 是自己当初刚学的时候犯得错误 就是 srpingmvc 把Service层的类 也给扫描了,然后注册到容器 但是 在srping.xml里面配置的事务,是没法用到springmvc扫描的bena里,也会报这种错误,为自己的不熟悉,付出了一下午的debug
引用 5 楼 rui888 的回复:
解决了就好 。哈哈
我还是有问题

<aop:config> 
    <aop:pointcut expression="execution(public * com.sxt.service.*.*(..))" id="businessService"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 
是做什么用的? 为什么com.sxt.service.*.* 我改成com.sxt.dao.*.* 或改成com.*.*.*.* 程序一样正常?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧