开发者社区> 问答> 正文

getHibernateTemplate().save() 无法插入数据 自动报错?报错

用的是SSH  但执行getHibernateTemplate().save()一直无法插入数据  (按照书上例子写的代码) 进行插入操作就跳throw JDBCExceptionHelper.convert错误

弄了好几天了  求助论坛各位大神   能帮我看下问题在哪里吗  网上查错也查不出个头绪 


跳转到的报错文件AbstractReturningDel(这个报错文件不是我写的 自动跳过来的)

package org.hibernate.id.insert;

import org.hibernate.id.PostInsertIdentityPersister;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.pretty.MessageHelper;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Abstract InsertGeneratedIdentifierDelegate implementation where the
 * underlying strategy causes the enerated identitifer to be returned as an
 * effect of performing the insert statement.  Thus, there is no need for an
 * additional sql statement to determine the generated identitifer.
 *
 * @author Steve Ebersole
 */
public abstract class AbstractReturningDelegate implements InsertGeneratedIdentifierDelegate {
	private final PostInsertIdentityPersister persister;

	public AbstractReturningDelegate(PostInsertIdentityPersister persister) {
		this.persister = persister;
	}

	public final Serializable performInsert(String insertSQL, SessionImplementor session, Binder binder) {
		try {
			// prepare and execute the insert
			PreparedStatement insert = prepare( insertSQL, session );
			try {
				binder.bindValues( insert );
				return executeAndExtract( insert );
			}
			finally {
				releaseStatement( insert, session );
			}
		}
		catch ( SQLException sqle ) {
			throw JDBCExceptionHelper.convert(
					session.getFactory().getSQLExceptionConverter(),
			        sqle,
			        "could not insert: " + MessageHelper.infoString( persister ),
			        insertSQL
				);
		}
	}

	protected PostInsertIdentityPersister getPersister() {
		return persister;
	}

	protected abstract PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException;

	protected abstract Serializable executeAndExtract(PreparedStatement insert) throws SQLException;

	protected void releaseStatement(PreparedStatement insert, SessionImplementor session) throws SQLException {
		session.getBatcher().closeStatement( insert );
	}
}
映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	 <class catalog="LakeWater" name="domain.userinfo.UserInfo" schema="dbo" table="User_Info">
		  <id name="UserID" type="java.lang.Integer">
		   <column name="UserID"/>
		   <generator class="native"/>
		  </id>
		  
		  <property  name="UserName" type="java.lang.String">
		   <column length="128" name="UserName"/>
		  </property>
		  <property  name="RealName" type="java.lang.String">
		   <column length="128" name="RealName"/>
		  </property>
		  <property  name="Password" type="java.lang.String">
		   <column length="128" name="Password"/>
		  </property>
		  <property  name="Email" type="java.lang.String">
		   <column length="100" name="Email"/>
		  </property>
		  <property  name="Telephone" type="java.lang.String">
		   <column length="30" name="Telephone"/>
		  </property>
		  <property  name="CompanyName" type="java.lang.String">
		   <column length="150" name="CompanyName"/>
		  </property>
		  <property  name="CreateTime" type="java.util.Date">
		   <column  name="CreateTime"/>
		  </property>
		  <property  name="LastLoginTime" type="java.util.Date">
		   <column name="LastLoginTime"/>
		  </property>
	 </class>
</hibernate-mapping>
DAO文件

package dao.userinfo;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import domain.userinfo.UserInfo;

public class UserInfoDAOImpl extends HibernateDaoSupport implements UserInfoDAO {
	// 检索所有管理员信息
	public List<UserInfo> findAll() {
		try {
			String queryString = "from UserInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public void save(UserInfo transientInstance) {
		try {
                       //此处报错
			getHibernateTemplate().save(transientInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}
Service

package service.userinfo;

import java.util.List;

import dao.userinfo.UserInfoDAO;
import domain.userinfo.UserInfo;

public class UserInfoFacadeImpl implements UserInfoFacade {
	
	private UserInfoDAO userInfoDAO;
	
	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	public List<UserInfo> findUserInfo() {
		// TODO Auto-generated method stub
		return userInfoDAO.findAll();
	}
	
	//添加新的管理员
	public boolean newUserInfo(UserInfo userinfo) {
		try {
			userInfoDAO.save(userinfo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}


}
Action

package sunyang.webtier;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import domain.userinfo.UserInfo;
import service.userinfo.UserInfoFacade;

public class UserInfoAction extends DispatchAction{
	private UserInfoFacade userInfoFacade;

	public UserInfoFacade getUserInfoFacade() {
		return userInfoFacade;
	}

	public void setUserInfoFacade(UserInfoFacade userInfoFacade) {
		this.userInfoFacade = userInfoFacade;
	}
	
	// 新增管理员
	public ActionForward addUserInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dyForm = (DynaActionForm) form;//定义一个动态ActionForm对象
		String Password = (String) dyForm.get("Password");// 取得密码
		String Email = (String) dyForm.get("Email");
		String UserName = (String) dyForm.get("UserName");
		String RealName = (String) dyForm.get("RealName");
		String Telephone = (String) dyForm.get("Telephone");
		String CompanyName = (String) dyForm.get("CompanyName");
	        UserInfo u = new UserInfo();
		u.setCompanyName(CompanyName);
		u.setEmail(Email);
		u.setPassword(Password);
		u.setRealName(RealName);
		u.setTelephone(Telephone);
		u.setUserName(UserName);	
		userInfoFacade.newUserInfo(u);//问题所在  注意!
		System.out.println("----------");
		ActionForward actForward = mapping.findForward("addSuccess");
		String path = actForward.getPath();
		path += "?method=findUserInfo";//需要传递的参数
		return new ActionForward(actForward.getName(), path, true);//防止刷新提交		
	}
}

application.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-2.0.xsd">
	<!-- 定义数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		destroy-method="close">
		<!-- 指定连接数据库驱动 -->
		<property name="driverClassName"
			value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
		<!-- 指定连接数据库url -->
		<property name="url"
			value="jdbc:microsoft:sqlserver://localhost:1433" />
		<!-- 指定连接数据库用户名,密码为空 -->
		<property name="username" value="sa" />
		<property name="password" value="sa" />
	</bean>
	<!-- 定义Hibernate的sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 指定数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 指定Hibernate映射文件 -->
		<property name="mappingResources">
			<list>
				<value>domain/userinfo/UserInfo.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<!-- 指定使用方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
				<!-- 是否在控制台输出sql语句 -->
				<prop key="show_sql">false</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.jdbc.batch_size">20</prop>
			</props>
		</property>
	</bean>
	<!-- 定义事务管理 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 定义事务管理拦截器 -->
	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<!-- 定义代理自动管理事务 -->
	<bean id="ProxyCreator"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<!-- 指定需要Spring管理事务的Bean -->
		<property name="beanNames">
			<list>
				<value>userInfoFacade</value>
			</list>
		</property>
		<!-- 调用事务管理拦截器 -->
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>
	
	<!--把DAO注入session工厂-->
	<bean id="userInfoDAO" class="dao.userinfo.UserInfoDAOImpl">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	
	<!--把service注入给DAO-->
	<bean id="userInfoFacade" class="service.userinfo.UserInfoFacadeImpl">
		<property name="userInfoDAO" ref="userInfoDAO"></property>
	</bean>

	<!--把Action注入给service-->
	<bean name="/back/userInfo" class="sunyang.webtier.UserInfoAction">
		<property name="userInfoFacade" ref="userInfoFacade"></property>
	</bean>

</beans>

展开
收起
爱吃鱼的程序员 2020-06-22 19:22:43 492 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    数据库是否有响应表和表结构?如果没有试试:<preclass="brush:xml;toolbar:true;auto-links:false;"><propkey="hibernate.hbm2ddl.auto">create</prop>回复<aclass='referer'target='_blank'>@exa:不是必须这样的,应该是你数据库和程序的配置不统一造成的回复<aclass='referer'target='_blank'>@E-leven11:的确行了啊!太感谢了我原来直接是update是因为这个原因吗?得先改成create?在换成update?回复<aclass='referer'target='_blank'>@exa:然后再改回update,看看是否可以正常插入您好按照您的方法试了一下数据库那张表的信息全部清空了....是在application.xml里面进行添加吗?我是冲着标题来的,“自动”回复<aclass='referer'target='_blank'>@helloming:嗯google查过了没有解决但楼上的方法试了可以解决了多谢你了回复<aclass='referer'target='_blank'>@exa:你把错误提示放到google上去查查吧感谢回复啊真的不太明白为什么会这样就取了个这样的标题异常贴出来异常就是第一个文件啊运行的是myeclipse执行插入程序就直接跳到AbstractReturningDel.java中的throwJDBCExceptionHelper.convert直接转到这个报错数据无法正确添加到数据库页面也不能正确跳转怎么不贴错误堆栈信息?....那不是错误堆栈信息...异常就是第一个文件啊运行的是myeclipse执行插入程序就直接跳到AbstractReturningDel.java中的throwJDBCExceptionHelper.convert不好意思不知道怎么直接@所以就在评论里面回复了

    2020-06-22 19:23:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
移动与复制 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载