title: Simple file upload using Flex UI and Spring/Hibernate/BlazeDS backend?
body: <p>I'm new to Flex and BlazeDS and I'm trying to implement a simple application which uses Flex on the front end and a Spring/Hibernate application on the back end, with communication between the two going over a BlazeDS channel.</p>
  
  <p>I'm seeking direction as to the best and/or simplest way to approach this.  I have the UI set up in such a way that the user is presented with a file chooser in which they pick the image file they want to upload.  When this is chosen and submitted (as a form submission) then the server side should receive the image file data as well as some related metadata such as a description and date, then populate a Hibernate entity/POJO with the image file data and related metadata, and then persist the entity/POJO into the database.</p>
  
  <p>I have found some examples of how you would do a file upload and download using servlets <a href="http://www.adobe.com/devnet/flex/articles/file_upload.html" rel="nofollow noreferrer">here</a> and the FileReference class (<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_7.html" rel="nofollow noreferrer">here</a> and <a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cf6.html" rel="nofollow noreferrer">here</a>) but these don't appear to address the problem in a way which leverages BlazeDS and/or Spring/Hibernate.  I want to put the image file data and related metadata (description, capture date, etc.) into a value object within the Flex application and then send this over BlazeDS to a service provided by my Spring/Hibernate application running on Tomcat.  In this service I want to extract the image data (both the actual JPG/PNG/GIF data and the related metadata such as description, etc.) from the value object sent from the Flex app into an entity/POJO which is then persisted via Hibernate in my database. </p>
  
  <p>Can this be done, and if so what's the best way to go about it?  Am I mistaken in assuming that if I use BlazeDS then I am somehow bypassing the need to provide HTTP-based services such as servlets on the server side and instead I can use my Java services as "RemoteObjects"?  Is there necessarily a one-to-one mapping between Java POJO/entity class and the Flex value object class when making this sort of transfer?  If so is there a tool which creates corresponding Flex value objects from Java POJOs or vice versa.</p>
  
  <p>Thanks in advance for your help, comments, suggestions, etc.</p>
  
  <p>--James</p>
  
  <p><strong>Update</strong>: Some code to make this more clear:</p>
  
  <p>I have this as my value object in Flex:</p>
  
  <pre><code>package valueobjects
  {
      import flash.utils.ByteArray;
  
      [Bindable]
      [RemoteClass(alias="com.abc.example.persistence.entity.Image")]
  
      public class Image
      {
          public var id:Number;
          public var captureDate:Date;
          public var description:String;
          public var imageData:ByteArray;
  
          public function Image() {}        
      }
  </code></pre>
  
  <p>I am assuming that this can be used as a one-to-one mapping to the POJO class used by my service and DAO classes on the server-side, which looks like this:</p>
  
  <pre><code>package com.abc.example.persistence.entity;
  
  import java.sql.Blob;
  import java.util.Date;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  
  @Entity(name = "IMAGE")
  public class Image
      extends AbstractBaseEntity&lt;Long&gt;
  {
      private String description;
      private Date captureDate;
      private Blob imageData;
  
      @Column(name = "CAPTURE_DATE", nullable = true)
      public Date getCaptureDate()
      {
          return captureDate;
      }
  
      @Column(name = "DESCRIPTION", nullable = true)
      public String getDescription()
      {
          return description;
      }
  
      @Column(name = "IMAGE_DATA", nullable = true)
      public Blob getImageData()
      {
          return imageData;
      }
  
      public void setCaptureDate(final Date captureDate)
      {
          this.captureDate = captureDate;
      }
  
      public void setDescription(final String description)
      {
          this.description = description;
      }
  
      public void setImageData(final Blob imageData)
      {
          this.imageData = imageData;
      }
  }
  </code></pre>
  
  <p>In my Flex application I populate the fields of an Image object with a description string, date, and image file data (based on the user's file selection and text input for the description) and then call a method on the RemoteObject which is mapped to the service running on Tomcat.  I make the RemoteObject service call within my Flex code using the Image value object as the argument, but the service method running on the servier side actually expects an argument of the POJO/entity type, and it's here that I am thinking that some sort of conversion/transformation between the Flex value object and the Java POJO will occur (by virtue of the RemoteClass alias setting on the value object's class declaration), but it doesn't seem to be happening that way because when I debug the application the Java service is only getting null values when the service call is made.</p>
  
  <p>In my Flex application I have a FileReference and Image value object as public, bindable variables:</p>
  
  <pre><code>[Bindable]
  public var imageToBeArchivedFileReference:FileReference = new FileReference();
  [Bindable]
  public var imageToBeArchivedValueObject:valueobjects.Image = new valueobjects.Image();
  </code></pre>
  
  <p>There is also an event handler to browse for a file when the user clicks on a file select button: </p>
  
  <pre><code>protected function imageFileSelectButton_clickHandler(event:MouseEvent):void
  {
      var imageFileFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
      var fileTypes:Array = new Array();
      fileTypes.push(imageFileFilter);
      imageToBeArchivedFileReference.addEventListener(Event.SELECT, imageToBeArchived_fileSelectHandler);
      imageToBeArchivedFileReference.browse(fileTypes);
  }
  </code></pre>
  
  <p>There is an event handler which builds the value object when the image file has been selected:</p>
  
  <pre><code>private function imageToBeArchived_fileSelectHandler(event:Event):void
  {
      imageToBeArchivedFileReference.load();
      imageToBeArchivedValueObject = new valueobjects.Image()
      imageToBeArchivedValueObject.imageData = imageToBeArchivedFileReference.data;
      imageToBeArchivedValueObject.description = imageToBeArchivedDescription.text;
      imageToBeArchivedValueObject.captureDate = imageToBeArchivedFileReference.creationDate;
  }
  </code></pre>
  
  <p>and there's an event handler which is invoked when the user clicks on the submit button to perform the image save/upload:</p>
  
  <pre><code>protected function archiveImageButton_clickHandler(event:MouseEvent):void
  {
      imageArchivalService.archiveImage(imageToBeArchived);
  }
  </code></pre>
  
  <p>On the server side my Java class is doing a simple save of the POJO:</p>
  
  <pre><code>public void archiveImage(final Image image)
  {
      imageDao.saveOrUpdate(image);
  }
  </code></pre>
  
  <p>When I set a breakpoint in the method above and look at the image variable it looks to be empty, so I'm assuming that the transformation from the Flex value object to the Java POJO did not go as expected and that there's more to it than just adding a RemoteClass alias in the Flex value object class.   </p>