title: How to extract images (lob) from Postgresql in a Spring Boot app (Hibernate + JPA)?
body: <p>I have a logic that takes images from f-end via byte[] in java, compress them and then store in Postgresql db in a column define with data lob.</p>
  <p>Service:</p>
  <pre><code> public CourseDto createNewCourse(CourseDto newCourseDto) throws SQLException {
  
      Courses course = courseRepositoryDao.findByCourseName(newCourseDto.getCourseName());
      if (course == null) {
  
          course = new Courses()
                  .setCourseName(newCourseDto.getCourseName())
                  .setCourseDescription(newCourseDto.getCourseDescription())
                  .setCoursePrice(newCourseDto.getCoursePrice())
                  .setIsCourseFree(newCourseDto.getIsCourseFree())
                  .setIsCourseActive(newCourseDto.getIsCourseActive())
                  .setLogo(compressZLib(newCourseDto.getLogo()));
  
          ;
          return CourseMapper.toUserDtoFreeCourses(courseRepositoryDao.save(course));
      }
      throw exception(EntityType.NEWCOURSE, ExceptionType.DUPLICATE_ENTITY, newCourseDto.getCourseName());
  }
  
  // compress the image bytes before storing it in the database
  public static byte[] compressZLib(byte[] data) {
      Deflater deflater = new Deflater();
      deflater.setInput(data);
      deflater.finish();
  
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
      byte[] buffer = new byte[1024];
      while (!deflater.finished()) {
          int count = deflater.deflate(buffer);
          outputStream.write(buffer, 0, count);
      }
      try {
          outputStream.close();
      } catch (IOException e) {
      }
      System.out.println(&quot;Compressed Image Byte Size - &quot; + outputStream.toByteArray().length);
  
      return outputStream.toByteArray();
  }
  </code></pre>
  <p>I try to retrieve the images:</p>
  <pre><code>    public List&lt;CourseDto&gt; getCoureses() {
  
          List&lt;Courses&gt; courses = courseRepositoryDao.findAllByIsCourseFreeAndIsCourseActive(true, true);
          List&lt;CourseDto&gt; coursesNameDto = courses
                  .stream()
                  .peek(i -&gt; i.setLogo(decompressZLib(i.getLogo())))
                  .map(course -&gt; modelMapper.map(CourseMapper.toUserDtoFreeCourses(course), CourseDto.class)).collect(Collectors.toList());
          System.out.println(&quot;**************************&quot; + coursesNameDto + &quot;**********************&quot;);
          return coursesNameDto;
      }
      // uncompress the image bytes before returning it to the angular application
      public static byte[] decompressZLib(byte[] data) {
          Inflater inflater = new Inflater();
          inflater.setInput(data);
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
          byte[] buffer = new byte[1024];
          try {
              while (!inflater.finished()) {
                  int count = inflater.inflate(buffer);
                  outputStream.write(buffer, 0, count);
              }
              outputStream.close();
          } catch (IOException ioe) {
          } catch (DataFormatException e) {
          }
          return outputStream.toByteArray();
      }
  </code></pre>
  <p>But I have this error: &quot;Objects may not be used in auto-commit mode&quot;</p>
  <p>My entity class has this field on logo:</p>
  <pre><code>@Column(name = &quot;picByte&quot;, length = 4000)
  @Lob
  private byte[] logo;
  </code></pre>