日期:2014-05-17  浏览次数:20812 次

使用HibernateTemplate无法将是实体类持久化到数据库
练习SSH,但程序好像执行都UserDao的hibernateTemplate.save(u)的上一句就不执行了,请高手指点……
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/photosystem">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<!-- <value>com.sun.java.entity/Photo.hbm.xml</value>
<value>com.sun.java.entity/Photoword.hbm.xml</value>
<value>com.sun.java.entity/Kind.hbm.xml</value> -->
<value>com/sun/java/entity/User.hbm.xml</value>
<!-- <value>com.sun.java.entity/Album.hbm.xml</value> -->
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
 
<bean id="userDao" class="com.sun.java.dao.UserDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="userService" class="com.sun.java.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
 
<bean id="register" class="com.sun.java.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>

</beans>

UserAction:
package com.sun.java.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.java.entity.User;
import com.sun.java.service.UserService;

public class UserAction extends ActionSupport{
private static Log log = LogFactory.getLog(UserAction.class);
private UserService userService;
private String username;
private String password;
private String password2;

public String register(){

if(password.equals(password2) && username != null) {
log.info("开始注册");
User u = new User();
u.setUsername(username);
u.setPassword(password);
u.getPassword();
userService.add(u);
return "registerSuccess";
}
return ERROR