title: uploading image into database using prime faces andhibernate
body: <p>I want to upload the image using primefaces and hibernate but it is giving the following error when i did this example simple JDBC connection but i need it in Hibernate. When i tried it hibernate then it also need to covert when i tried this as given below then it is giving error. if any one have idea then please help</p>
  
  <pre><code>exception 
  
  javax.servlet.ServletException: java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
      org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:77)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  
  root cause 
  
  javax.faces.el.EvaluationException: java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
      javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
      com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
      javax.faces.component.UICommand.broadcast(UICommand.java:311)
      javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
      javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
      com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
      com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
      org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:77)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  
  root cause 
  
  java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
      org.hibernate.type.BlobType.set(BlobType.java:49)
      org.hibernate.type.BlobType.nullSafeSet(BlobType.java:117)
      org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1947)
      org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1924)
      org.hibernate.persister.entity.AbstractEntityPersister$1.bindValues(AbstractEntityPersister.java:2102)
      org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:32)
      org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2108)
      org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2588)
      org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
      org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
      org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
      org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
      org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
      org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
      org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
      org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
      org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
      org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
      org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
      org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
      org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
      com.school.entityDao.SchoolStudentDao.saveStudent(SchoolStudentDao.java:37)
      com.school.entity.SchoolStudent.saveStudent(SchoolStudent.java:381)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      org.apache.el.parser.AstValue.invoke(AstValue.java:278)
      org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
      com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
      javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
      com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
      javax.faces.component.UICommand.broadcast(UICommand.java:311)
      javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
      javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
      com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
      com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
      org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:77)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  
  
  The code i have done for the uploading for hibernate is
  
  private UploadedFile file;
  
      private byte[] image;
  
      public byte[] getImage() {
          return image;
      }
  
      public void setImage(byte[] image) {
          this.image = image;
      }
  
      public UploadedFile getFile() {
          return file;
      }
  
      public void setFile(UploadedFile image) {
          this.file = image;
      }
  
  
  
  
  
  the above one is for the Managed bean and for saving is
  
  public void saveStudent(SchoolStudent schoolStudent)
      {
          Transaction trans=null;
          Session session= HiberUtil.getSessionFactory().openSession();
          File f=new File("C:\\Users\\Web It Experts\\Desktop\\New Instructions\\20.jpg");
          byte[] imageData=new byte[(int) f.length()];
          try{
              trans=session.beginTransaction();
              FileInputStream fileInputStream = new FileInputStream(f);
              fileInputStream.read(imageData);
              fileInputStream.close();
  
  
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
          schoolStudent.setImage(imageData);
          session.save(schoolStudent);
          session.getTransaction().commit();
      }
  </code></pre>