springmvc+hibernate整合事务不回滚,谁来拯救我

最近心血来潮研究下了springmvc,发现比struts2好用多了,配置也方便,捣鼓了一阵,最后想把hibernate也整进去,结果悲剧就来了,事务就是不回滚,实在没招了,哪位大侠给看下,上代码

springmvc-servlet.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byName"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">














class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">









class="org.springframework.web.servlet.view.InternalResourceViewResolver">







class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />






applicationContext-db.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">

























${hibernate.dialect}
true
none
org.hibernate.search.store.FSDirectoryProvider
100
100
true
manual
















applicationContext-aop.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="byName">
































UserServiceImpl
@Service(value = "userService")
@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService {

@Autowired
private UserDAO userDAO;

@Override
public void addUser(User user) {
userDAO.addUser(user);
}

}

UserDAOImpl
@Repository(value = "userDAO")
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

private Logger logger = Logger.getLogger(getClass());

@Override
public void addUser(User user) {
logger.debug("新增用户");
this.getHibernateTemplate().save(user);
}

UserServiceTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:webroot/WEB-INF/springmvc-servlet.xml","classpath:applicationContext-*.xml"})
public class UserServiceTest{

@Autowired
private UserService userService;

@Test
public void testAddUser() {

User user1 = new User();
user1.setCode("A002");
user1.setName("haha");
userService.addUser(user1);

User user2 = new User();
user2.setCode("A001");
user2.setName("haha");
userService.addUser(user2);

User user3 = new User();
user3.setCode("A001");//code是唯一的,这里报唯一约束错误,前面两条数据是不是应该插入失败?
user3.setName("haha");
userService.addUser(user3);
}

}

就在这里找不到问题了,use3前面写个更新也不行,user2还是会被更新,网上说扫描controller和service的时候分开,我也分开了,难道是DAO有问题还是怎么的,还是说配置有问题,望来位高人指点一二 :cry:

你可能感兴趣的:(springmvc)