SpringMVC3.2+Hibernate4存储数据出错原因

今天纠结了我一天的数据库数据存不上终于找到原因了:

配置文件如下:

web.xml:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:config/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:config/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<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>/*</url-pattern>
	</filter-mapping>

applicationContext.xml:

<!-- 配置数据源 -->  
	<bean id="dataSource"  
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />  
		<property name="url" value="jdbc:mysql://127.0.0.1/jlmedicine" />  
		<property name="username" value="root" />  
		<property name="password" value="root" />  
	</bean> 
	<!--  配置hibernate SessionFactory-->  
	<bean id="sessionFactory"  
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.jl.medicine.model"/>  
		<property name="hibernateProperties">  
			<props>  
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
				<prop key="hibernate.hbm2ddl.auto">update</prop>  
				<prop key="hibernate.show_sql">true</prop>  
				<prop key="hiberante.format_sql">true</prop>
				<prop key="current_session_context_class">thread</prop>  
			</props>  
		</property>
	</bean>
	<bean id="transactionManager"  
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="find*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED</prop>
				<prop key="execute*">PROPAGATION_REQUIRED</prop>
				<prop key="load*">PROPAGATION_REQUIRED</prop>
				<prop key="merge*">PROPAGATION_REQUIRED</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean> 

spring-mvc.xml:

<context:annotation-config />
    <!--使Spring支持自动检测组件,如注解的Controller -->
    <context:component-scan base-package="*">  
    </context:component-scan>
    <!-- 开启注解 -->
	<mvc:annotation-driven/>
	<!-- 静态资源访问 -->
	 <mvc:default-servlet-handler/>	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		  <property name="defaultEncoding" value="utf-8" />
	      <property name="maxUploadSize" value="10485760000" />
	      <property name="maxInMemorySize" value="40960" />
	</bean>

然后开始配置DAO层和Service层具体代码就不贴了,网上一大堆。

在用SessionFactory的currentSession时,save方法死活不执行,在网上试了各种方法,差一点就放弃这个框架了。

网上都是说要commit一下,但是我用Transaction的commit时后台报错,是因为我配置文件中根本就没有配置这玩意,按照网上说的配tx标签配了半天也不好用。

下了好多这个框架的源码都是直接调用save就能保存视频也是这样,我非常蛋疼。。。。一度怀疑我人品不咋的。

终于找到了一篇文章,说的是hibernate执行sql的insert语句失败是因为没有flush,于是我就flush了一下,竟然存进去了。。。。。。

好吧,我也不知道是我配置的问题还是我人品的问题,反正我这中配置的currentSession的save方法就是要flush一下要不就是只执行了SQL而没有commit。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值