Data lost because of JPA AttributeConverter?
<p>I use JPA to persist my data to the database. In detail I use Hibernate 4.3.5 as JPA implementation.
Because of performance and keeping the table structure simple, I don not map directly 1:1 from objects to tables but some list of data I only keep as objects but do not create entity classes for. Instead of this, I serialize the object structure as JSON to the DB. This serialization/deserialization is done with @Converter and works mostly nice.</p>

<p>Simplified Code:</p>

<pre><code>@Entity
public class EntitySample {
  ...
  @Convert(converter=ConverterSample.class)
  private List&lt;SampleObject&gt; sampleList=new ArrayList&lt;&gt;();

  private String name;

  public List&lt;SampleObject&gt; getSampleList() {
    return sampleList;
  }

  public void setName(String newName) {
    name=newName;
  }
  ...
}

@Converter
public class ConverterSample implements AttributeConverter&lt;List,String&gt; {

  @Override
  public String convertToDatabaseColumn(List data) {
    return serializeToJSON(data);
  }

  @Override
  public List convertToEntityAttribute(String data) {
    return deserializeFromJSON(data);
  }

  ...
}
</code></pre>

<p>As told, it is mostly working!
I detected the following problem in a unit test:</p>

<pre><code>// create a new entity object with list A, B, C:
EntitySample entity=new EntitySample();
entity.getSampleList().add(new SampleObject("A"));
entity.getSampleList().add(new SampleObject("B"));
entity.getSampleList().add(new SampleObject("C"));
entity.setName("init");
startTransaction();
getEM().persist(entity);
commitTransaction();

// change the order to A, C, B:
getEM().clear();
EntitySample loaded=getEM().find(...); // just reload from DB
SampleObject moveObj=loaded.getSampleList().remove(1);
loaded.getSampleList().add(moveObj);
// loaded.setName("changed"); // all works with this change, but not without!

startTransaction();
getEM().merge(loaded);
commitTransaction();
</code></pre>

<p>With the upper code, an object with a JSON list with elements A, B, C is written to DB. After this the object is loaded back again and the order of the list elements is changed to A, C, B. But now saving the object does NOT change data data in the database! For me it looks like, that Hibernate does not detect that anything has changed! My converter code convertToDatabaseColumn() is not called for the merge.
but as soon as I also change the name (commented line above), all works fine. Now the loaded object seems to be detected as changed and therefore also the conversion is called and the JSON string is stored to DB.</p>

<p>Does anybody know this bug or knows a workaround? Or is it my bug in the end?</p>