【发布时间】:2015-06-11 10:49:19
【问题描述】:
我正在为 java 开发一个带有 play 2.3(带有 Ebean 和 H2)的程序。我有一个这样的模型:
@Entity
public class DeviceModel extends Model implements PathBindable<DeviceModel> {
@Id
public Long id;
@Lob
@Basic(fetch=FetchType.LAZY)
public byte[] picture;
...
在我的控制器中,我有一个函数,它在DeviceModel 对象内将图片写为byte[] 并调用update() 函数。所以现在应该将图片保存在数据库中。
我有这个功能来显示图片:
public static Result picture(Long id) {
final DeviceModel deviceModel = DeviceModel.findByID(id);
if (deviceModel == null){
return notFound();
}
return ok(deviceModel.picture);
}
有趣的是deviceModel.picture 为空!
但在我看来,我有这个:
@if(deviceModel.picture != null) {
show the picture!
} else{
do something else
}
但是在这里,deviceModel.picture 不为空!!!大多数情况下,图片都会正确显示!!
我删除了@Basic(fetch=FetchType.LAZY),但并没有解决问题。
任何想法为什么会这样?
【问题讨论】:
-
只是猜测:您的模板实际上是使用 getPicture() 方法而不是直接访问该字段。也许你在这个方法中有一些额外的代码以某种方式转换图片的字节[]?
-
感谢您的提示。作为一种解决方案,我将我的图片字段设为私有,并设置了我自己的 getter 和 setter。现在用 getPicture() 我总是能得到数据
标签: java playframework h2 playframework-2.3 ebean