Question

i'm trying to change my File entity from byte[] model to Blob.

Original code:

@Entity
@Table(name="raw_data")
public class RawDataEntity extends DomainObject {
  @Lob
  private byte[] data;

New code:

@Entity
@Table(name="raw_data")
public class RawDataEntity extends DomainObject {

    @Lob
    private Blob data;

It works fine, BUT I need to use this entity as Seriazable - use in Wicket pages.. (I can't use a DetachableModel or remove Serialization, because, there is a large model of hibernate entities which have to be serialized..)

Hopefully one of you can help me out.

Was it helpful?

Solution

You can make the Blob transient and then it won't be serialized but it will be null upon deserialization:

private transient Blob data;

I think your only other option is to create a separate object for the blob and only reference it in the RawDataEntity and the use a LoadableDetachableModel to reload it upon request.

public class BlobModel extends LoadableDetachableModel<Blob> {

    private int id;

    public BlobModel(int id) {
        this.id = id;
    }

    @Override
    protected Blob load() {
        return dao.getObject(id);
    }
}

An other option is change the serialization for pages that cannot be serialized. First create an interface for those pages:

public interface CustomSerialize {
    Serializable getSerializable(Object object);
    Object getUnserializable(Object object);
}

Next create your own serializer:

public class CustomSerializer extends JavaSerializer {
public CustomSerializer(String applicationKey) {
    super(applicationKey);
}

@Override
public byte[] serialize(Object object) {
    if (object instanceof CustomSerialize) {
        super.serialize(((CustomSerialize) object).getSerializable(object));
    }
    return super.serialize(object);
}

@Override
public Object deserialize(byte[] data) {
    Object object = super.deserialize(data);
    if (object instanceof CustomSerialize) {
        return ((CustomSerialize) object).getUnserializable(object);
    }
    return object;
}
}

Then implement that interface on a page which cannot be serialized and write the two methods. getSerializable() should create an object that implements CustomSerializer so that getUnserializable() return the same page.

and last, use the new serializer in your wicket WebApplication:

    getFrameworkSettings().setSerializer(new CustomSerializer(getApplicationKey()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top