spring3.2+hibernate4.2.8 整合 报错,求指教!

sunvim 发布于 2013/12/12 16:46
阅读 564
收藏 2

我的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 <context:annotation-config />
 <context:component-scan base-package="test.sunsc.*" />

  <!-- 配置Datasource -->
 <bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <value>/conf/comm.properties</value>
  </property>
 </bean>

 <bean id="dataSource" destroy-method="close"
  class="org.apache.commons.dbcp.BasicDataSource"
  p:driverClassName="${jdbc.driverClassName}"
  p:url="${jdbc.url}"
  p:username="${jdbc.username}"
     p:password="${jdbc.password}"
     p:initialSize="10"
     p:maxOpenPreparedStatements="10"  
     p:maxActive="255"   
     p:maxIdle="2"   
     p:maxWait="120000"/>

 <!-- 配置sessionFactory -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
   <!-- 将dataSource注入给sessionFactory -->
  <property name="dataSource" ref="dataSource" />
  <property name="mappingResources">
   <list>
    <value>test/sunsc/table/hibernateUser.hbm.xml</value>
   </list>
  </property>
   <!-- 配置Hibernate的一些属性 -->
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="current_session_context_class">thread</prop>
   </props> 
  </property>
 </bean>

<!-- 声明事务管理器,将我们定义的sessionFactory注入 -->
 <bean id="txManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
<!-- aop指定好切面,指定切面逻辑为txAdvice -->
 <aop:config proxy-target-class="true"/>
 <aop:config>
  <aop:pointcut id="bussinessService"
   expression="execution(* test.sunsc.dao..*.*(..))" />
  <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
 </aop:config>
 
<!-- 在切面逻辑中指定事务管理器为txManager -->
 <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"/>
            <!--hibernate4必须配置为开启事务 否则 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>

</beans>

 

 

代码:

 

public class HibernateUtil {
 private static SessionFactory  sf;
 private static ApplicationContext ctx;
 
 public static SessionFactory getSessionFactory(){
  ctx = new FileSystemXmlApplicationContext("conf/appConfig.xml");  
  sf = (SessionFactory) ctx.getBean("sessionFactory");  
  return sf;
 }

}

public class UserDaoImpl  implements UserDao {

 public void save(HibernateUser user) {


  SessionFactory sf =  HibernateUtil.getSessionFactory();
  Session s =sf.getCurrentSession();
  s.beginTransaction();
  s.save(user);
  s.getTransaction().commit();
  s.close();
 }

红色部分,为报错部分,不知道如何解决!

以下是问题补充:

@sunvim:报的是 no session current found (2013/12/12 22:01)
加载中
0
akeng
akeng
getCurrentSession()不行就openSession()嘛
sunvim
sunvim
应该是配置的问题,但不知问题出在哪里
akeng
akeng
回复 @sunvim : 配置的问题把,当前线程没有session你还怎么拿
sunvim
sunvim
呵呵,我要用事务,所以我觉得用getCurrentSession方便一些! 现在遇到问题,就想弄哥究竟!
0
糊涂的酒精
糊涂的酒精

<prop key="current_session_context_class">thread</prop>

改成:

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

试试

0
peanutmain
peanutmain
spring都没有开始事务,怎么通过spring来获取到current session,好比ssh中你想在struts的action方法中获取current session,个人认为~~
sunvim
sunvim
说的对,处理模块,事务没有开启导致的! 对整个框架理解欠火候!
0
sxgkwei
sxgkwei
个人用的体会,hi4就是个坑。还是换回3吧。
sunvim
sunvim
喜欢迎难而上,总算解决问题了!
0
糊涂的酒精
糊涂的酒精

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>


打错了,是这个……

sunvim
sunvim
嗯,谢谢!
0
yongzhong
yongzhong

修改你的web.xml文件相关内容为如下

<!-- Spring MVC Servlet -->
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>



<!-- open session filter -->
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>sessionFactory</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



spring mvc的拦截和hibernate的session拦截路径是不一样的,但/*和/的区别我没搞清楚具体是什么,这样配置下可以解决这个问题,我也遇到过

sunvim
sunvim
谢谢,问题解决了! 只要在所用模块,加上事务 就可以了!
返回顶部
顶部