getHibernateTemplate().save() 不保存数据
- Java code
<!--spring配置--><?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 连接池dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driverClassName}"> </property> <property name="url" value="${url}"> </property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> <!-- 最大 连接数 --> <property name="maxActive" value="100"></property> <!--最大空闲连接 --> <property name="maxIdle" value="30"></property> <!-- 最大等待连接 --> <property name="maxWait" value="500"></property> <!-- 默认最大提交,TRUE,每操作一次数据库自动提交 <property name="defaultAutoCommit" value="true"></property>--> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <!--ernate方言--> <prop key="hibernate.dialect"> ${dialec} </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">none</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop> <!--统计信息 --> <prop key="generate_statistics">false</prop> <!-- Query查询时也用二级缓存 <prop key="cache.use_query_cache">true</prop>--> </props> </property> <!-- <property name="mappingResources"> <list> <value>com/test/bean/User.hbm.xml</value> </list> </property>--> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="find" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置哪些类哪些方法使用事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.teamsun.drp.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> </aop:config></beans><!--Dao--> /** * 增加用户信息 */ public Boolean addUser(TblDrpUser entity) throws HibernateException { Boolean flag = false; try{ this.getHibernateTemplate().saveOrUpdate(entity); flag = true; }catch(Exception e){ e.printStackTrace(); throw new HibernateException("添加用户信息出错!"); } return flag; }
控制台信息:
13:46:40,484 INFO [STDOUT] Hibernate: select tbldrpuser0_.User_id as User1_17_0_, tbldrpuser0_.User_name as User2_17_0_, tbldrpuser0_.User_info as User3_17_0_, tbldrpuser0_.passwd as passwd17_0_, tbldrpuser0_.Rolegroup_id as Rolegroup5_17_0_, tbldrpuser0_.operator as operator17_0_, tbldrpuser0_.created_ts as created7_17_0_, tbldrpuser0_.Upt_ts as Upt8_17_0_, tbldrpuser0_.Login_allowed as Login9_17_0_ from drp.tbl_drp_user tbldrpuser0_ where tbldrpuser0_.User_id=?
13:46:40,515 INFO [STDOUT] Hibernate: select count(*) as col_0_0_ from drp.tbl_drp_user tbldrpuser0_
13:46:40,531 INFO [STDOUT] Hibernate: select tbldrpuser0_.User_id as User1_17_, tbldrpuser0_.User_name as User2_17_, tbldrpuser0_.User_info as User3_17_, tbldrpuser0_.passwd as passwd17_, tbldrpuser0_.Rolegroup_id as Rolegroup5_17_, tbldrpuser0_.operator as operator17_, tbldrpuser0_.created_ts as created7_17_, tbldrpuser0_.Upt_ts as Upt8_17_, tbldrpuser0_.Login_allowed as Login9_17_ from drp.tbl_drp_user tbldrpuser0_ limit ?
13:46:40,546 INFO [STDOUT] Hibernate: select tbldrprole0_.Rolegroup_id as Rolegroup1_15_0_, tbldrprole0_.Rolegroup_name as Rolegroup2_15_0_, tbldrprole0_.Rolegroup_info as Rolegroup3_15_0_, tbldrprole0_.Created_ts as Created4_15_0_, tbldrprole0_.Upd_ts as Upd5_15_0_ from drp.tbl_drp_rolegroup tbldrprole0_ where tbldrprole0_.Rolegroup_id=?
13:46:40,546 INFO [STDOUT] Hibernate: select tbldrprole0_.Rolegroup_id as Rolegroup1_15_0_, tbldrprole0_.Rolegroup_name as Rolegroup2_15_0_, tbldrprole0_.Rolegroup_info as Rolegroup3_15_0_, tbldrprole0_.Created_ts as Created4_15_0_, tbldrprole0_.Upd_ts as Upd5_15_0_ from drp.tbl_drp_rolegroup tbldrprole0_ where tbldrprole0_.Rolegroup_id=?
13:46:40,546 INFO [STDOUT] Hibernate: select tbldrprole0_.Rolegroup_id as Rolegroup1_15_0_, tbldrprole0_.Rolegroup_name as Rolegroup2_15_0_, tbldrprole0_.Rolegroup_info as Rolegroup3_15_0_, tbldrprole0_.Created_ts as Created4_15_0_, tbldrprole0_.Upd_ts as Upd5_15_0_ from drp.tbl_drp_rolegroup tbldrprole0_ where tbldrprole0_.Rolegroup_id=?
13:46:40,562 INFO [STDOUT] Hibernate: select tbldrprole0_.Rolegroup_id as Rolegroup1_15_0_, tbldrprole0_.Rolegroup_name as Rolegroup2_15_0_, tbldrprole0_.Rolegroup_info as Rolegroup3_15_0_, tbldrprole0_.Created_ts as Created4_15_0_, tbldrprole0_.Upd_ts as Upd5_15_0_ from drp.tbl_drp_rolegroup tbldrprole0_ where tbldrprole0_.Rolegroup_id=?
13:46:42,062 INFO [STDOUT] Hibernate: select count(*) as col_0_0_ from drp.tbl_drp_rolegroup tbldrprole0_
13:46:42,078 INFO [STDOUT] Hibernate: select tbldrprole0_.Rolegroup_id as Rolegroup1_15_, tbldrprole0_.Rolegroup_name as Rolegroup2_15_, tbldrprole0_.Rolegroup_info as Rolegroup3_15_, tbldrprole0_.Created_ts as Created4_15_, tbldrprole0_.Upd_ts as Upd5_15_ from drp.tbl_drp_rolegroup tbldrprole0_ limit ?
13:46:42,078 INFO [STDOUT] com.teamsun.drp.po.TblDrpUser@1014dd4
[解决办法]
this.getHibernateTemplate().saveOrUpdate(entity);
是保存就save 是更新就update ,我不知道这个方法有没有出问题,但不建议这么做
[解决办法]
应该是回滚了
把 Rollback 设置成 false 就可以了
[解决办法]
可以参考一下这里的内容;
使用Hibernate处理数据(转载)
Hibernate不能保存中文
[解决办法]
lz把dao类粘出来吧
[解决办法]
[解决办法]
首先 public Boolean addUser(
这个方法所在的类要继承HibernateTemplateSupport
然后再把再在applicationContext.xml把这个类注入到IOC里这个标签里设置
<property name="sessionFactory"><ref local="sessionFactory"/></property>
[解决办法]
大概是配置文件哪里出错了 saveOrUpdate方法是可以的