code: private void onPersistGFS(Object entity, Object entityId, EntityMetadata entityMetadata, boolean isUpdate)
      {
          KunderaGridFS gfs = new KunderaGridFS(mongoDb, entityMetadata.getTableName());
          if (!isUpdate)
          {
              GridFSInputFile gfsInputFile = handler.getGFSInputFileFromEntity(gfs, entityMetadata, entity,
                      kunderaMetadata, isUpdate);
              saveGridFSFile(gfsInputFile, entityMetadata);
          }
          else
          {
              Object val = handler.getLobFromGFSEntity(gfs, entityMetadata, entity, kunderaMetadata);
              String md5 = MongoDBUtils.calculateMD5(val);
              GridFSDBFile outputFile = findGridFSDBFile(entityMetadata, entityId);
  
              // checking MD5 of the file to be updated with the file saved in DB
              if (md5.equals(outputFile.getMD5()))
              {
                  DBObject metadata = handler.getMetadataFromGFSEntity(gfs, entityMetadata, entity, kunderaMetadata);
                  outputFile.setMetaData(metadata);
                  outputFile.save();
              }
              else
              {
                  // GFSInput file is created corresponding to the entity to be
                  // merged with a new ObjectID()
                  GridFSInputFile gfsInputFile = handler.getGFSInputFileFromEntity(gfs, entityMetadata, entity,
                          kunderaMetadata, isUpdate);
                  ObjectId updatedId = (ObjectId) gfsInputFile.getId();
                  DBObject metadata = gfsInputFile.getMetaData();
  
                  // updated file is saved in DB
                  saveGridFSFile(gfsInputFile, entityMetadata);
  
                  // last version of file is deleted
                  DBObject query = new BasicDBObject("_id", outputFile.getId());
                  gfs.remove(query);
  
                  // newly added file is found using its _id
                  outputFile = gfs.findOne(updatedId);
  
                  // Id of entity (which is saved in metadata) is updated to its
                  // actual Id
                  metadata.put(((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName(), entityId);
                  outputFile.setMetaData(metadata);
  
                  // output file is updated
                  outputFile.save();
              }
          }
      }
docs: On persist GFS.
  
  @param entity
  the entity
  @param entityId
  the entityId
  @param entityMetadata
  the entity metadata
  @param isUpdate
  the is update