Serializing List to JSON Array
<p>I have a JPA entity with a List of custom objects as one of its fields. Using a Jackson converter, I've managed to persist this list as a JSON array into a MySQL database, but Iam unable to insert into this list after its initial creation.</p>
<p>I can successfully retrieve the existing list, add a new object in memory(and test that it has been inserted), then save it via a Spring REST repository. However, it never seems to persist. Any ideas? Here is my code (this is a Spring Boot project FYI):</p>
<p><strong>Candidate entity with a List inside</strong></p>
<pre><code>@Entity
@Table(name = &quot;Candidates&quot;, schema = &quot;Candidate&quot;)
public class Candidate extends ResourceSupport {

@Id
@Column(name = &quot;CandidateID&quot;)
private Long candidateID;

// More fields

@Column(name = &quot;Fields&quot;)
@Convert(converter = CollectionConverter.class)
private List&lt;CandidateField&gt; fields;

//Getters &amp; setters
}
</code></pre>
<p><strong>CandidateField class which makes up the List above. The CandidateField is simply a POJO that models the JSON stored in a single field in the Candidate table, it is not an independent entity.</strong></p>
<pre><code>public class CandidateField {

private Long fieldID;
private String name;
private boolean current;

public CandidateField () {

}

public CandidateField (Long fieldID, String name, boolean current) {
    this.fieldID = fieldID;
    this.name = name;
    this.current = current;
}

//Getters &amp; Setters
}
</code></pre>
<p><strong>Converter</strong></p>
<pre><code>public class CollectionConverter implements AttributeConverter&lt;List&lt;CandidateField&gt;, String&gt; {

private ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(List&lt;CandidateField&gt; object) {
    try {
        return objectMapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return &quot;&quot;;
    }
}

@Override
public List&lt;CandidateField&gt; convertToEntityAttribute(String data) {
    try {
        return objectMapper.readValue(data, new TypeReference&lt;List&lt;CandidateField&gt;&gt;() {});
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
</code></pre>
<p><strong>Code that persists to database</strong></p>
<pre><code>public void addField(Long fieldID, Long candidateID) {
    Candidate candidate = repository.findOne(candidateID);
    candidate.getFields().add(new CandidateField(fieldID, &quot;&quot;, true));
    repository.saveAndFlush(candidate);
}
</code></pre>
<p><strong>Repository</strong></p>
<pre><code>@RepositoryRestResource
public interface CandidateRepository extends JpaRepository&lt;Candidate,Long&gt;{}
</code></pre>
<p>I can't seem to figure out why this won't persist. Any help will be very much appreciated. Cheers!</p>