<bean id="framework.support.hibernate.sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
lazy-init="false">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${framework.hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${framework.hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${framework.hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
</prop>
<!-- cache -->
<prop key="net.sf.ehcache.configurationResourceName">${framework.net.sf.ehcache.configurationResourceName}</prop>
<prop key="hibernate.cache.use_second_level_cache">${framework.hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${framework.hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${framework.hibernate.cache.provider_class}</prop>
<prop key="hibernate.cache.region.factory_class">${framework.hibernate.cache.region.factory_class}</prop>
</props>
</property>
<property name="dataSource" ref="framework.support.dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/hzyl</value>
</list>
</property>
</bean>我在sping 配置
<bean id="sampleService" class="com.hzyl.sample.service.SampleService" init-method="init" />
init 方法会查询数据库,sessionFactory.getCurrentSession();的时候会抛出一下异常,
但如果不用init 方法,等程序启动完毕之后再查询数据库就不会org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at com.hzyl.framework.orm.hibernate.HibernateEntityManager.getSession(HibernateEntityManager.java:60)
at com.hzyl.framework.orm.hibernate.HibernateEntityManager.find(HibernateEntityManager.java:143)
at com.hzyl.sample.service.SampleService.init(SampleService.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
lazy-init="false">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${framework.hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${framework.hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${framework.hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
</prop>
<!-- cache -->
<prop key="net.sf.ehcache.configurationResourceName">${framework.net.sf.ehcache.configurationResourceName}</prop>
<prop key="hibernate.cache.use_second_level_cache">${framework.hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${framework.hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${framework.hibernate.cache.provider_class}</prop>
<prop key="hibernate.cache.region.factory_class">${framework.hibernate.cache.region.factory_class}</prop>
</props>
</property>
<property name="dataSource" ref="framework.support.dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/hzyl</value>
</list>
</property>
</bean>我在sping 配置
<bean id="sampleService" class="com.hzyl.sample.service.SampleService" init-method="init" />
init 方法会查询数据库,sessionFactory.getCurrentSession();的时候会抛出一下异常,
但如果不用init 方法,等程序启动完毕之后再查询数据库就不会org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at com.hzyl.framework.orm.hibernate.HibernateEntityManager.getSession(HibernateEntityManager.java:60)
at com.hzyl.framework.orm.hibernate.HibernateEntityManager.find(HibernateEntityManager.java:143)
at com.hzyl.sample.service.SampleService.init(SampleService.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
解决方案 »
- 怎么更改该XML文件中的节点<lastSave></lastSave> 插入值?如:<lastSave>12345</lastSave>
- 一个很奇怪的错误(刚从公司导回的项目运行不了)
- 关于利用现有数据库进行开发问题,请前辈指点
- web service 接口问题,用的是xfire
- 关于页面关闭时执行jsp代码
- 在tomcat中运行struts工程出现这样的错 哪位帮忙解决一下 不胜感激
- JSP网页怎样实现, 如MSN那样的信息提示窗口
- tld配置文件的关于tag和function合在一起配置的问题
- 动态级联下拉菜单
- jsp中如何动态的include文件
- 请教大家一些jdbc和DB2编程的问题!
- struts2.xml的action中的result报错
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="framework.support.hibernate.sessionFactory" />
</bean> <aop:config proxy-target-class="true">
<aop:pointcut id="productServiceMethods"
expression="execution(* com.hzyl..*Service.*(..))"/>
<aop:advisor advice-ref="framework.support.hibernate.transactionAdvice" pointcut-ref="productServiceMethods"/>
</aop:config> <tx:advice id="framework.support.hibernate.transactionAdvice" transaction-manager="framework.support.hibernate.transactionManager">
<tx:attributes>
<tx:method name="persist*" read-only="false" propagation="REQUIRED" />
<tx:method name="delete*" read-only="false" propagation="REQUIRED" />
<tx:method name="import*" read-only="false" propagation="REQUIRED" />
<tx:method name="save*" read-only="false" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="list*" read-only="true" propagation="REQUIRED"/>
<tx:method name="count*" read-only="true" propagation="REQUIRED"/>
<tx:method name="init*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
在线等 求解
似乎是不符合 execution(* com.hzyl..*Service.*(..)) 设置