Spring4+Hibernate4事务小记
学习Spring+Hibernate,,非常强大的框架,为了追新,就直接从最高版本开始学习了,这要冒很大的风险,因为网上可查到的资料大多是针对旧版本的,比如Spring3,Hibernate3。
根据我的测试,记录如下:
1.Spring4事务必须针对接口,否则报错。(之前可以不针对接口?)如果没有事务,则bean普通类也可以注入。
2.事务可以放在接口上,也可以放在实现上。
3.注意应用程序空间必须统一,否则事务失败。
4.Bean最好都交给Spring来管理,如果不是,则如果出现事务控制,可能因不同的应用空间而造成事务失败。
5.事务连接可以使用DataSourceTransactionManager,使用HibernateTransactionManager也可以。
<!–<bean><property ref=”dataSource” /></bean>class
6.如果使用了JdbcTemplate和Hibernate,则应使用HibernateTransactionManager,如果使用DataSourceTransactionManager,则会提示没有活动Session错误。
(这个可能是我配置的有问题?)
7.如果使用事务,则调用时一定要使用接口,否则就会报告错误。
BillOperbaseBill=(BillOper)ctx.getBean(“saleBill”);
BillOper为第一没接口。
8.如果在程序中像这样使用:
sessionFactory.getCurrentSession().save(T);
如果不开启事务,则会报告
No Session found for current thread
错误。
9.Spring4+Hibernate4中不再支持HibernaterTemplate,只能使用原生的session接口来操作。
10.如果报告错误:hibernate:Cannot use identity column key generation with <union-subclass> mapping for…..
使用hibernate union-subclass 元素进行继承映射时会抛出此异常:Cannot use identity column key generation with <union-subclass> mapping for………….
原因是使用unnion-subclass 元素时 父类的标识属性生成器不能使用“indentity(native)” 可以选择”hilo”,”increment”等。
11.如果使用Spring+Hibernate,有时Hibernate的配置文件错误不容易发现(至少我没有找到好办法),可以直接使用Hibernate来调试。
12.有时在测试框架(junit)中无法显示Spring配置文件错误,可以在单独的程序单元中进行测试。
绝对新手,欢迎大侠们对不当的地方指正!
posted on
Copyright ©2013 garfieldtom Powered by: 博客园 模板提供:沪江博客
人生就像是一场旅行,遇到的既有感人的,