Hibernate4.1.8 整合Spring,Spring 3.1.2管理事务
请问Dao中是用什么方法拿SessionFactory 呢?
继承Hibernate HibernateDaoSupport 没用因为 Spring只支持hibernate3 后面就不支持了
请问Dao中是用什么方法拿SessionFactory 呢?
继承Hibernate HibernateDaoSupport 没用因为 Spring只支持hibernate3 后面就不支持了
解决方案 »
- 请大家给推荐几个JSP的开源CMS系统
- 关于servlet编译
- IE不能显示swf格式的文件的参数,ie应该怎样设置
- java.lang.ClassCastException: testjpa.book.WebBookstoreBooks cannot be cast to testjpa.book.WebBooks
- 用LambdaProbe监控Tomcat的问题,用过的请进!!!!
- jsp 和 servlet的数据传递问题
- 在windows2000下测试通过的servlet,在linux下为什么找不到?请大侠帮忙!!!解决立即给分
- Java 中关于JSplitPane的问题?
- spring4配置hibernate4 hibernate.hbm2ddl.auto无效
- frameset 怎么添加背景图片,在整个主页面里。。麻烦具体代码
- 网页即时更新问题,求解答
- 一个令人纠结的问题 数字格式化!
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory; public Session getSession() {
//事务必须是开启的(Required),否则获取不到
return sessionFactory.getCurrentSession();
}
applicationcontext.xml中
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${database.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com/test/demo/bean</value><!-- 扫描实体类,也就是平时所说的model -->
</list>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" propagation="REQUIRED"/>
<!--hibernate3必须配置为开启事务 否则 getCurrentSession()获取不到-->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.test.*.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
你参照这修改把,