Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 948
behzad Avatar asked Mar 08 '26 23:03

behzad


1 Answers

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

...
like image 115
behzad Avatar answered Mar 11 '26 13:03

behzad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!