我的配置文件:
<?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();
}
红色部分,为报错部分,不知道如何解决!
把
<propkey="current_session_context_class">thread</prop>
改成:
<prop key=" hibernate.current_session_context_class "> org.springframework.orm.hibernate4.SpringSessionContext</prop>
试试
spring都没有开始事务,怎么通过spring来获取到currentsession,好比ssh中你想在struts的action方法中获取currentsession,个人认为~~说的对,处理模块,事务没有开启导致的!对整个框架理解欠火候!个人用的体会,hi4就是个坑。还是换回3吧。喜欢迎难而上,总算解决问题了!<prop key=" hibernate.current_session_context_class ">org.springframework.orm.hibernate4.SpringSessionContext</prop>
打错了,是这个……
修改你的web.xml文件相关内容为如下
<!--SpringMVCServlet--><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>版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。