Hibernate4 将 Entity 保存不到数据库 但可以查询数据问题解决

<span style="color:#ff0000;">applicationContext.xml </span>
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:jaxws="http://cxf.apache.org/jaxws"   
	xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-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/context      
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/cache 
                    http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
	<!-- 这里设置了不扫描的Annotation的类型,
	这是因为org.springframework.stereotype.Controller是SpringMVC的控制器的注解,
	使用这个注解注册的Bean在SpringMVC容器启动的时候已经实例化了,所以在Spring容器里面就不需要进行实例化了
		 -->
	<context:component-scan  base-package="com"  >
		 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- 数据源配置, 使用 BoneCP 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
		<property name="driverClassName" value="${driverClassName}" />

		<!-- 基本属性 url、user、password -->
		<property name="url" value="${url}" />
		<property name="username" value="root" />
		<property name="password" value="root" />

		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="${initialSize}" />
		<property name="minIdle" value="${minIdle}" />
		<property name="maxActive" value="${maxActive}" />

		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="60000" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />

		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />

		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />

		<!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用) <property name="poolPreparedStatements" 
			value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" 
			value="20" /> -->

		<!-- 配置监控统计拦截的filters -->
		<property name="filters" value="stat" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.ajie.base.common.entity</value>
				<value>com.ajie.base.web.entity</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
				</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="cache.use_second_level_cache">true</prop>
				<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
				</prop>
				<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
				<!-- 在hibernate3的时候是thread和jta   <prop key="current_session_context_class">thread</prop>-->		
			</props>
			<!-- <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.format_sql=true 
				hibernate.show_sql=true hibernate.hbm2ddl.auto=update<# # spaces here for 
				exception> </value> -->
			<!-- hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.current_session_context_class=org.hibernate.context.internal.JTASessionContext 
				hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.SunOneJtaPlatform -->
		</property>
		<!--<property name="jtaTransactionManager"> <ref bean="transactionManager"/> 
			</property> -->
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<!-- 新添加,处理session报错 <tx:annotation-driven transaction-manager="transactionManager" 
		/> -->
	<!--配置事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>

			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />

			<tx:method name="*" propagation="SUPPORTS"  />
		</tx:attributes><pre name="code" class="html"><?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:jaxws="http://cxf.apache.org/jaxws"  xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-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/context      
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/cache 
                    http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
					http://www.springframework.org/schema/mvc 
                	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<!-- 属性文件位置 -->
	<mvc:annotation-driven />
	<!--  
	<mvc:default-servlet-handler/>
	-->
	<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 
	<context:component-scan base-package="com">
		 
			  <context:exclude-filter type="regex"
			expression="com.ajie.*..controller" />
	</context:component-scan>
	-->	
	
	 <context:component-scan base-package="com">
	  <context:include-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
       <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Service" />
  </context:component-scan>
	<!-- 开启controller注解支持 -->
  <!-- 注:如果base-package=com.avicit 则注解事务不起作用 TODO 读源码
  这里配置了扫描Controller,通过Controller注解注册的Bean是SpringMVC的控制器,但是为什么要排除Service注解呢?这是因为通过Service注册的Bean是要进行事务处理的。要生成动态代理进行事务控制,所以如果不排除的话,Service注册的Bean是不带事务处理的。所以在整合Hibernate的时候就会报没有事务的异常
  -->
  <context:component-scan base-package="com">
    <context:include-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
    <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Service" />
  </context:component-scan>
	 
	
	<!-- 静态文件访问 -->
	<mvc:resources mapping="/static/**" location="/static/" />
	<bean id="contentNegotiatingViewResolver"
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="contentNegotiationManager">
			<bean
				class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
				<property name="mediaTypes">
					<props>
						<prop key="atom">application/atom+xml;charset=UTF-8</prop>
						<prop key="xml">application/xml;charset=UTF-8</prop>
						<prop key="html">text/html;charset=UTF-8</prop>
						<prop key="json">application/json;charset=UTF-8</prop>
					</props>
				</property>
			</bean>
		</property>

		<property name="defaultViews">
			<list>
				<bean
					class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
				</bean>
			</list>
		</property>

		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
				<bean
					class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="viewClass"
						value="org.springframework.web.servlet.view.JstlView" />
					<property name="prefix" value="/WEB-INF/jsp/" />
					<property name="suffix" value=".jsp" />
				</bean>
			</list>
		</property>
	</bean>
</beans>

</tx:advice><!--那些类的哪些方法参与事务 --><aop:config><aop:advisor pointcut="execution(* com.ajie.base.web.service.*Service.*(..))"advice-ref="txAdvice" /><aop:advisorpointcut="execution(* com.ajie.base.web.service.impl.*ServiceImpl.*(..))"advice-ref="txAdvice" /> <aop:advisorpointcut="execution(* com.ajie.base.common.service.*Service.*(..))"advice-ref="txAdvice" /> <aop:advisorpointcut="execution(* com.ajie.base.common.service.impl.*ServiceImpl.*(..))"advice-ref="txAdvice" /></aop:config></beans>
 

dispatcher-servlet.xml 


<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:jaxws="http://cxf.apache.org/jaxws"  xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-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/context      
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/cache 
                    http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
					http://www.springframework.org/schema/mvc 
                	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<!-- 属性文件位置 -->
	<mvc:annotation-driven />
	<!-- 开启controller注解支持 -->
  <!-- 注:如果base-package=com.avicit 则注解事务不起作用 TODO 读源码
  这里配置了扫描Controller,通过Controller注解注册的Bean是SpringMVC的控制器,但是为什么要排除Service注解呢?这是因为通过Service注册的Bean是要进行事务处理的。要生成动态代理进行事务控制,所以如果不排除的话,Service注册的Bean是不带事务处理的。所以在整合Hibernate的时候就会报没有事务的异常
  -->
  <context:component-scan base-package="com">
    <context:include-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
    <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Service" />
  </context:component-scan>
	 
	
	<!-- 静态文件访问 -->
	<mvc:resources mapping="/static/**" location="/static/" />
	<bean id="contentNegotiatingViewResolver"
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="contentNegotiationManager">
			<bean
				class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
				<property name="mediaTypes">
					<props>
						<prop key="atom">application/atom+xml;charset=UTF-8</prop>
						<prop key="xml">application/xml;charset=UTF-8</prop>
						<prop key="html">text/html;charset=UTF-8</prop>
						<prop key="json">application/json;charset=UTF-8</prop>
					</props>
				</property>
			</bean>
		</property>

		<property name="defaultViews">
			<list>
				<bean
					class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
				</bean>
			</list>
		</property>

		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
				<bean
					class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="viewClass"
						value="org.springframework.web.servlet.view.JstlView" />
					<property name="prefix" value="/WEB-INF/jsp/" />
					<property name="suffix" value=".jsp" />
				</bean>
			</list>
		</property>
	</bean>
</beans>

仔细看 这两个xml  的 context:component-scan    springMvc 那个xml 也就是 dispatcher-servlet.xml  必须排除service 看注解 就行,主要是 这块弄好了,hibernate4就保存到数据库了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值