File

For large binary or text content (database LOB) we can model it in the entity bean as a property of type java.io.File and with @Lob.

@Entity
public class SomeFileBean {
  ...

  @Lob
  File content;

Lazy fetched by default

By default all @Lob properties are fetched lazy so that means by default they are NOT fetched unless explicit included in the select clause.

Note that File properties (and other Lob properties) will lazy load as needed.

QSomeFileBean b = QSomeFileBean.alias()

SomeFileBean bean =
  new QSomeFileBean()
    .select(b.name, b.content)
    .id.eq(42)
    .findOne();

File content = bean.getContent();

Content streamed to file

For each File returned Ebean creates a temporary file and streams the content from the database Lob result into the file. As such when we fetch beans with Files we are expected to process the file in some way and then potentially discard, delete or move the temporary file. If we do not then this can result in a build up of temporary files in the temp directory.

Insert

// have the content as a File
File tempFile = ...;

UserProfile profile = new UserProfile();
profile.setName("Rob");
profile.setProfileImage(tempFile);

database.save(profile);

// typically tidy up file
tempFile.delete();

Fetch

QUserProfile u = QUserProfile.alias()

UserProfile profile =
  new QUserProfile()
    .select(u.name, u.profileImage)
    .id.eq(42)
    .findOne();

File image = profile.getProfileImage();
// do something with the image file
// maybe move it to a cache directory
// or tidy up by deleting the temporary file
image.delete();

Stateless update

It is often useful to perform a stateless update where we update the File content without fetching it.

// have the content as a File
File updatedProfileImage = ...;

UserProfile profile = new UserProfile();
profile.setId(42);
profile.setProfileImage(updatedProfileImage);

// perform update without fetching
database.update(profile);

// typically tidy up file
updatedProfileImage.delete();