开发者社区> 问答> 正文

Spring framework3.2整合hibernate4.1报错:No S?报错

  最近在自己捣鼓这个东西,使用的spring-framework-3.2.0 和 hibernate4.1。

  想自己写个通用dao,结果在getCurrentSession()的时候报错了。但是使用openSession()是正常的。

   请教下各位OSCER,这是什么情况,谢谢。

/**
	 * Hibernate Session工厂
	 */
	@Autowired
	@Qualifier("sessionFactory")  
	private SessionFactory  sessionFactory;
	
	protected Session d(){
		return sessionFactory.getCurrentSession();
	}
  下面是配置信息

<?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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx    
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	<!-- 数据源管理 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/MyDB?useUnicode=true&characterEncoding=UTF-8" />
		<property name="username" value="" />
		<property name="password" value="" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.beiyu" />
		<property name="hibernateProperties">
			<props>
				<prop key="current_session_context_class">thread</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.format_sql">true</prop>
				<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
			</props>
		</property>
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
            <tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <!--默认其他方法都是REQUIRED-->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
	
	<!-- 组件扫描路径 -->
	<context:component-scan base-package="com.beiyu.*" />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/classes/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

   在网上找了很多资料,有的说是要加

<prop key="current_session_context_class">thread</prop>
但是加了也不可以。。。。
在线等各位回答,谢谢!

展开
收起
爱吃鱼的程序员 2020-06-22 22:29:46 469 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <propertyname="dataSource"ref="dataSource"/>
    </bean>
    <tx:adviceid="txAdvice"transaction-manager="txManager">
    <tx:attributes>
    <tx:methodname="save*"propagation="REQUIRED"/>
    <tx:methodname="add*"propagation="REQUIRED"/>
    <tx:methodname="create*"propagation="REQUIRED"/>
    <tx:methodname="insert*"propagation="REQUIRED"/>
    <tx:methodname=""read-only="true"/>
    </tx:attributes>
    </tx:advice>

    <aop:configproxy-target-class="true">
    <aop:advisoradvice-ref="txAdvice"pointcut="execution(net.noday..service...
    (..))"/>

    </aop:config>

    这样才是完整的事物配置,你少了aop的配置所以没有事物

    我没加这一段:<aop:configproxy-target-class="true"><aop:advisoradvice-ref="txAdvice"pointcut="execution( net.noday..service... (..))"/></aop:config>回去了试试。谢谢

    没事物也会这样

    回复<aclass='referer'target='_blank'>@李烈火:你是说,在protectedSessiond(){}上面加上事务注解?这个方法我试了,同样的不行那就加上事物,curentsession必须在事物中是的啊。没使用事务呢。

    <spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;">getCurrentSession需要做如下配置:

    <spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;">

    <preclass="brush:xml;toolbar:true;auto-links:false;"><propertyname="current_session_context_class">thread</property>

    <spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;">强烈推荐看看这篇文章:

    <spanstyle="font-family:Arial;font-size:14px;line-height:26px;background-color:#FFFFFF;"><atarget="_blank"rel="nofollow">HibernateSessionFactory中openSession和getCurrentSession方法的区别

    回复<aclass='referer'target='_blank'>@汉唐:我先看下这篇文章,谢谢已经加了的,但是还不可以。

    <prop<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.649999618530273px;background-color:#FFFFFF;"> key="current_session_context_class">thread</prop>

    以上这段一定要去掉,然后配置文件加入如下(com.dao用你的dao包代替):

    <preclass="brush:xml;toolbar:true;auto-links:false;"><aop:configexpose-proxy="true"><aop:pointcutid="txPointcut"expression="execution(com.dao...(..))"/><aop:advisoradvice-ref="baseServiceAdvice"pointcut-ref="txPointcut"/></aop:config>


    好的。我回去试试,谢谢。<divclass="ref">

    引用来自“李烈火”的答案

    <divclass=ref_body><beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <propertyname="dataSource"ref="dataSource"/>
    </bean>
    <tx:adviceid="txAdvice"transaction-manager="txManager">
    <tx:attributes>
    <tx:methodname="save*"propagation="REQUIRED"/>
    <tx:methodname="add*"propagation="REQUIRED"/>
    <tx:methodname="create*"propagation="REQUIRED"/>
    <tx:methodname="insert*"propagation="REQUIRED"/>
    <tx:methodname=" "read-only="true"/>
    </tx:attributes>
    </tx:advice>

    <aop:configproxy-target-class="true">
    <aop:advisoradvice-ref="txAdvice"pointcut="execution(net.noday..service...
    (..))"/>

    </aop:config>

    这样才是完整的事物配置,你少了aop的配置所以没有事物

    <imgsrc="http://www.oschina.net/js/ke/plugins/emoticons/images/54.gif"alt=""/>,我也碰到这个了,但是aop我配置了的,求指教

    在serviceImpl层加@Transactional


    <spanstyle="color:#333333;font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:12px;line-height:normal;background-color:#FFFFEE;"><aop:configproxy-target-class="true"> 
    <spanstyle="color:#333333;font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:12px;line-height:normal;background-color:#FFFFEE;"><aop:advisoradvice-ref="txAdvice"pointcut="execution( net.noday..service... (..))"/> 

    </aop:config>


    谢谢

    Hibernate4方式改变了下,如果不指定@Transactional,需要在web.xml中指定

    <!--openSessionInView配置-->
       <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>
       </filter>
       <filter-mapping>
           <filter-name>openSessionInViewFilter</filter-name>
           <url-pattern>
    .html</url-pattern>
       </filter-mapping>

    web.xml未配置openSession。

    <!--配置Session-->
     <filter>
     <filter-name>openSession</filter-name>
     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
     </filter>
     <filter-mapping>
     <filter-name>openSession</filter-name>
     <url-pattern>/*</url-pattern>
     </filter-mapping>

    这个可以的

    2020-06-22 22:30:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多