spring4與hibernate5整合報錯解決


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/user"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">        
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">        
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

    <!--這里開啟了hibernate當中的線程session開關-->
            <prop key="hibernate.current_session_context_class">thread</prop>
        </props>        
        </property>
        <!-- 使用包掃描方式添加類 -->
        <property name="mappingResources">
             <list>
                <value>cn/hcong/vo/Person.hbm.xml</value>
             </list>
         </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" read-only="false"/>
            <tx:method name="update*" read-only="false"/>
            <tx:method name="delete*" read-only="false"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(public * cn.hcong.service.impl.*.*(..))" id="perform"/>
        <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
    </aop:config>
    
    <bean id="personDao" class="cn.hcong.dao.impl.PersonDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="personService" class="cn.hcong.service.impl.PersonServiceImpl">
        <property name="pd" ref="personDao"></property>
    </bean>

然后調用了測試代碼保存的代碼,發現報Caused by: org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction,這樣的錯誤,

原因就是將  <prop key="hibernate.current_session_context_class">thread</prop>這句話給刪除掉就好了,

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM