<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
lazy-init="false">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/aaa/healthHouse/model/Cusnation.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
--></props>
</property>
</bean>
这一段中,hibernate.current_session_context_class这一项不需要配置,当程序出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个错误时,可能是因为<tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>这段配置中的proxy-target-class中的值为true,即表示代理接口是指定的class,而不是接口代理,当我们在dao层的接口进行@Transaction注解时,会出现这个错误。
当我们将hibernate.current_session_context_class这一项的配置的值设为thread时,即使注解@Trsansaction注解到了接口并且指定代理在类,也能获得session,但是此时获得的session会出现 not actived transaction 即,当前上下文不存在活跃的事务,其实此时可能session不存于spring的管理中,解决的办法:
1、将<tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>中的proxy-target-class设为false,,默认就使用接口代理,即默认值即为false
2、@Transaction注解写在dao的实现类层,但是这样违背了我们面向接口编程的思想,不推荐