ssh 框架批量保存对象时hibernate报错
使用ssh框架,批量插入数据时报如下错误:
java.lang.RuntimeException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.DataException: Could not execute JDBC batch update
com.renren.web.action.AlbumAction.addPhotos(AlbumAction.java:137)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
批量保存的action:
AlbumForm af = (AlbumForm) form;
Users u= (Users)request.getSession().getAttribute("loginInfo");
String userId =u.getId().toString();
String albumId = request.getParameter("albumId");
Album album = (Album) photoServiceImp.findById(Album.class, Integer.valueOf(albumId));
String webSavePath = "/images/upload/"+u.getId()+"/album/"+albumId;
String upPath = request.getSession().getServletContext().getRealPath(webSavePath);
System.out.println(albumId+"--"+userId+"\r\n"+upPath+"\r\n");
System.out.println("文件个数:"+af.getAllPhoto().size());
//循环上传照片
for (int i = 0; i < af.getAllPhoto().size(); i++) {
Photo p = new Photo();
FormFile photo = af.getPhotos(i);
System.out.println(photo.getFileName());
try {
String saveName = MyFileStream.uploadFile(photo, upPath);
p.setAddTime(new Date());
p.setAlbum(album);
p.setPhoto(webSavePath+"/"+saveName);
System.out.println(webSavePath+"/"+saveName);
photoServiceImp.save(p);
System.out.println("添加的信息"+p.getId()+"--"+p.getAddTime());
System.out.println("Hashcode"+p.hashCode());
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException(e.getMessage());
}
}
//清空form中的photos数组列表
//af.clearPhotos();
return mapping.findForward("goOneAlbumUI");
photo表的主键生成策略是increment;实体类的配置文件,使用反向工程生成的。
为啥报批量更新失败,需要强制flush么???困扰许久
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查一下异常信息里的
caused by
语句抛出的具体的异常信息是啥,如果是反向工程的话,很有可能就是字段长度不够...出现这种情况可能是: