hibernate打印sql却没有插入数据库

J2EE 码拜 8年前 (2016-09-22) 1564次浏览
刚学ssh遇到的问题,hibernate打印sql却没有插入数据库,一开始网上查了一下,大部分人说事务没有配置事务自动提交,但是设置<prop key=”connection.autocommit”>true</prop>并没有解决问题,请论坛的各位高手不吝赐教,谢谢各位了~
以下是代码
<?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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd”>
<!– 加载jdbc配置文件 –>
<context:property-placeholder location=”classpath:jdbc.properties” />
<!– 加载c3p0数据连接池 –>
<bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”>
<property name=”driverClass” value=”${jdbc.driver}” />
<property name=”jdbcUrl” value=”${jdbc.url}” />
<property name=”user” value=”${jdbc.user}” />
<property name=”password” value=”${jdbc.password}” />
</bean>
<!– 加载hibernate配置文件 –>
<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<property name=”dataSource” ref=”dataSource” />
<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=”connection.autocommit”>false</prop>
<prop key=”connection.provider_class”>
org.hibernate.connection.C3P0ConnectionProvider
</prop>
<prop key=”c3p0.min_size”>5</prop>
<prop key=”c3p0.max_size”>20</prop>
<prop key=”c3p0.timeout”>120</prop>
<prop key=”c3p0.idle_test_period”>3000</prop>
</props>
</property>
<!– 加载hbm.xml –>
<property name=”mappingResources”>
<list>
<value>cn/itcast/user/User.hbm.xml</value>
</list>
</property>
</bean>
<!– 配置事务管理器 –>
<bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory” />
</bean>
<!– 注入action –>
<bean id=”indexAction” class=”cn.itcast.index.IndexAction” scope=”prototype”></bean>
<bean id=”userAction” class=”cn.itcast.user.UserAction” scope=”prototype”>
<property name=”userService” ref=”userService” />
</bean>
<!– 注入service –>
<bean id=”userService” class=”cn.itcast.user.UserService”>
<property name=”userDao” ref=”userDao” />
</bean>
<!– 注入dao –>
<bean id=”userDao” class=”cn.itcast.user.UserDao”>
<property name=”sessionFactory” ref=”sessionFactory” />
</bean>
<!– 配置事务注解驱动 –>
<tx:annotation-driven transaction-manager=”transactionManager” />
</beans>

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDao extends HibernateDaoSupport{
public void save(User user) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(user);
}
}

import org.springframework.transaction.annotation.Transactional;
import utils.UUIDUtils;
@Transactional
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void regist(User user) {
// TODO Auto-generated method stub
user.setState(0);
user.setCode(UUIDUtils.getUUID());
userDao.save(user);
}
}

控制台打印的结果:
Hibernate:
insert
into
user
(username, password, name, email, phone, addr, sex, state, code)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
解决方案

10

sessionfactory配置的有什么意义?

5

估计还是事务问题,再检查一下

5

spring事务

5

<!– 配置事务注解驱动 –>
<tx:annotation-driven transaction-manager=”transactionManager” />
这是一个bean代表的是一个通知,你的aop看下,可能哪里有些问题!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明hibernate打印sql却没有插入数据库
喜欢 (0)
[1034331897@qq.com]
分享 (0)