我正在为 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;
...
在我的 Controller 中,我有一个函数,它将图片作为 byte[]
写入 DeviceModel
对象并调用 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)
但它并没有解决问题。
知道为什么会这样吗?
最佳答案
我找到了解决此问题的方法,但我仍然想知道为什么直接访问图片字段会返回 null。
解决方法如下:
我只是将我的 picture
字段设为私有(private),并将 getter 和 setter 设为我自己。现在在我的 Controller 中,使用 getPicture()
我总能得到数据
@Entity
public class DeviceModel extends Model implements PathBindable<DeviceModel> {
@Id
public Long id;
@Lob
@Basic(fetch=FetchType.LAZY)
private byte[] picture;
public byte[] getPicture() {
return picture;
}
public void setPicture(byte[] picture) {
this.picture = picture;
}
...
关于java - Lob 使用 Play Framework 和 Ebean 和 H2 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30778708/