behzad behzad - 17 days ago 6
Java Question

Lob returns null using play framework and Ebean and H2

I am developing a program with play 2.3(with Ebean and H2) for java. I have a model like this:

@Entity
public class DeviceModel extends Model implements PathBindable<DeviceModel> {

@Id
public Long id;


@Lob
@Basic(fetch=FetchType.LAZY)
public byte[] picture;

...


In my controller I have a function which writes a picture as
byte[]
inside a
DeviceModel
object and calls the
update()
function. so now the picture should be saved in database.

And i have this function to show the picture:

public static Result picture(Long id) {
final DeviceModel deviceModel = DeviceModel.findByID(id);
if (deviceModel == null){
return notFound();
}
return ok(deviceModel.picture);
}


the funny thing is that
deviceModel.picture
is null!

but in my view, I have this:

@if(deviceModel.picture != null) {
show the picture!
} else{
do something else
}


but here,
deviceModel.picture
is not null!!! and MOST OF THE TIMES the picture will be shown correctly!!

I deleted the
@Basic(fetch=FetchType.LAZY)
but it didn't solve the problem.

any Idea why is it like this?

Answer

I found a work around for this issue, but I still like to know the reason, why accessing the picture field directly, returns null.

here is the work around: I just made my picture field private, and made getter and setter my self. now in my Controller, with getPicture() I always get the data

@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;
}

...
Comments