Java 上传图像时出现休眠异常

Java 上传图像时出现休眠异常,java,hibernate,Java,Hibernate,我的控制器类是 @RequestMapping(value = "/profilePictureUpload", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("fileExtension") String fileExtension, @RequestParam("file") MultipartFile file,HttpServletRequest request) { log

我的控制器类是

@RequestMapping(value = "/profilePictureUpload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("fileExtension") String fileExtension, @RequestParam("file") MultipartFile file,HttpServletRequest request) {

     logger.info("In add profile Picture Upload");  
     String mediaResponse=null;;
     try {
         String token = request.getHeader("authToken");
         System.out.println("+++++++++++++++token+++++++++++++++++++"+token );
            User user = userDao.findUserByAuthToken(token);
            System.out.println("+++++++++++++++USER+++++++++++++++++++"+user );
            System.out.println("+++++++++++++++file.getBytes()+++++++++++++++++++"+file.getBytes() );
            if (user != null) {

                  Physician physicain=physicainDao.findPhysicianById(user.getPhysicianId().getPhysicianId());
                  String fileStoragPath=userOriginalServerPath+"/"+physicain.getPhysicianId();
                  File file1=new File(fileStoragPath);
                  file1.mkdirs();
                  String filePath=fileStoragPath+"/"+physicain.getPhysicianId()+System.currentTimeMillis()+fileExtension;
                  FileOutputStream fileOuputStream =new FileOutputStream(filePath); 
                  fileOuputStream.write(file.getBytes());
                  fileOuputStream.close();  
                  /**
                   * Creating thumbnail for media upload
                   */
                  File thumbnailPath=new File(userThumbnailFilePath+physicain.getPhysicianId());
                  thumbnailPath.mkdirs();
                  String thumbnail_path=physicain.getPhysicianId()+"/"+physicain.getPhysicianId()+System.currentTimeMillis()+fileExtension;

                  Thumbnails.of(new File(filePath)).size(75, 75).toFile(new File(userThumbnailFilePath+thumbnail_path));
                  physicain.setProfileImage("MedikmUserPicture/thumbnail/"+thumbnail_path);
                  physicainDao.update(physicain);   
                  mediaResponse="MedikmUserPicture/thumbnail/"+thumbnail_path;

             }else{
                  mediaResponse=MedikmConstants.USER_INVALID_AUTHENTICATION__MESSAGE;

             } 
            System.out.println("================mediaResponse============"+mediaResponse);
            return mediaResponse;

        }
        catch(Exception ex){
            ex.printStackTrace();
            logger.error("Error in media tag Method :"+ex);
            return mediaResponse=MedikmConstants.USER_INVALID_AUTHENTICATION__MESSAGE;
        }

 }
并通过上传图像获得休眠异常,如

org.hibernate.exception.DataException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
图像已成功上传,但不知道为什么我会得到医生类的异常字段名“我映射了吗?”

 @Column(name = "Profile_Image")
private String profileImage;

请帮我从这个异常中解脱出来。

配置文件图像应该是TINYBLOBBLOBMEDIUMBLOBLONGBLOB

根据您的需要,如:

  • TINYBLOB:最大长度为255字节
  • BLOB:最大长度为65535字节
  • MEDIUMBLOB:最大长度为16777215字节
  • LONGBLOB:最大长度为4294967295字节
因此,您必须更新您的个人资料\u图像

示例:如果设置列MEDIUMBLOB,则其映射应为:-

@Lob
@Column(name="Profile_Image",columnDefinition="mediumblob")
private byte[] profileImage;
注意:我在保存一个非常大的图像时出现了这个错误,我的数据类型是Blob,我使用的是mysql
数据包太大。您可以通过设置max_allowed_packet变量在服务器上更改此值。

经过一番搜索,我发现

如果使用大BLOB列或 长串。它应该与您要使用的最大BLOB一样大。 允许的最大\u数据包的协议限制为1GB。值应该是 1024的倍数;非整数向下舍入到最接近的整数 多重

所以我发现
SET GLOBAL max_allowed_packet=1024*1024*MB数

i、 e

这将设置允许的最大数据包大小14 GB,请确保重新启动MySQL以使更改生效


请参阅和完整示例以在hibernate中保存图像

是否尝试将图像作为字符串上载?这是错误的。将其另存为字节数组。多部分文件具有getBytes方法,请使用它。其次,必须禁用批处理更新以查看实际错误并发布更多错误信息。最后,您的控制器方法中的逻辑太多,请将其移动到服务层。thakn您的回复异常是数据截断:2015-09-16 17:02:09598错误[org.hibernate.event.def.AbstractFlushingEventListener](http-apr-8080-exec-4)列“Profile_Image”的数据太长-无法将数据库状态与sessiono org.hibernate.exception.DataException同步:无法在org.hibernate.exception.sqlstatecoverter.convert(sqlstatecoverter.java:102)和org.hibernate.exception.jdbceptionhelper.convert(jdbceptionhelper.java:66)上执行JDBC批处理更新在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)中,如我所说,禁用配置中的批处理更新,这样您将正确地出现错误。另外,不要在评论中发布错误,编辑你的主要帖子并把它们放在那里。上次我没有在StringLast中保存图像,在主要帖子中发布错误日志,清理你的代码。
SET GLOBAL max_allowed_packet = 1024*1024*14