JPA: continuous data synchronization, necessary consultations

I am trying to create a JPA class that implements a simple interface:

public interface Task<T> {
    public void setData(T data);
    public T getData();
    public void run();
}

For some reason, I need it to be T datastored in serialized form, so my JPA class needs both JPA recipients / setters and an interface implementation. Obviously, they need to be somehow synchronized.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AbstractTask<T extends Serializable> implements Task<T> {
    private T data;
    private byte[] serializedData; // do I need both vars? which one to choose?

    @Override
    @Transient
    public T getData() {
        return data; // or deserialized 'serializedData'?
    }

    @Override
    public void setData(T data) {
        this.data = data; // or serialize into 'serializedData'?
    }

    @Column
    @Lob
    public byte[] getSerializedData() {
        return serializedData; // or serialized 'data'?
    }

    public void setSerializedData(byte[] bytes) {
        this.serializedData = bytes;
        // deserialize 'bytes' into 'data'?
    }
}

What is the right approach for synchronizing these class variables or how to rebuild one from the other? The question is a little broader than serialization / deserialization, and applies to any situation where an object can be uniquely rebuilt from persistent JPA data and vice versa.

+3
source share
1 answer

. PrePersist PostLoad .

+1

All Articles