title: JPA: persistent data synchronization, advice needed
body: <p>I'm trying to create JPA class implementing simple interface:</p>
  
  <pre><code>public interface Task&lt;T&gt; {
      public void setData(T data);
      public T getData();
      public void run();
  }
  </code></pre>
  
  <p>For some reasons I need <code>T data</code> to be stored in a serialized form, so my JPA class needs both JPA getters/setters and interface implementation. Obviously, they need to be synchronized somehow.</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  public abstract class AbstractTask&lt;T extends Serializable&gt; implements Task&lt;T&gt; {
      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'?
      }
  }
  </code></pre>
  
  <p>What is the correct approach for synchronizing these class variables or how to rebuild one from another? The question is a bit wider than serialization/deserialization and applies to any situation where some object could be uniquely rebuild from JPA persistent data and vice versa.</p>