ファイル
大規模なバイナリまたはテキストコンテンツ(データベースLOB)の場合、java.io.File
型のプロパティとしてエンティティBeanでモデル化し、@Lob
を使用できます。
@Entity
public class SomeFileBean {
...
@Lob
File content;
デフォルトで遅延フェッチ
デフォルトでは、すべての@Lob
プロパティは遅延フェッチされます。つまり、選択句に明示的に含まれない限り、デフォルトではフェッチされません。
ファイルプロパティ(およびその他のLobプロパティ)は必要に応じて遅延読み込みされることに注意してください。
QSomeFileBean b = QSomeFileBean.alias()
SomeFileBean bean =
new QSomeFileBean()
.select(b.name, b.content)
.id.eq(42)
.findOne();
File content = bean.getContent();
ファイルにストリーミングされるコンテンツ
Ebeanは返される各ファイルに対して一時ファイルを作成し、データベースのLob結果からファイルにコンテンツをストリーミングします。したがって、ファイルを使用したBeanをフェッチする場合、なんらかの方法でファイルを処理し、一時ファイルを破棄、削除、または移動することが期待されます。それを行わない場合、一時ディレクトリに一時ファイルが蓄積される可能性があります。
挿入
// 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();
フェッチ
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();
ステートレス更新
ファイルを取得せずにファイルのコンテンツを更新するステートレス更新を実行すると便利です。
// 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();