Payara 4.1.2.181 (and correponding eclipselink version)
Hello everyone,
I want to store as a json string a list of instances representing notes (as a string, a user id and a date) so I am using an attribute converter to convert from a List to a String :
@Converter(autoApply = true)
public class NotesAttributeConverter implements AttributeConverter<List<Note>, String> {
private Gson gson = new Gson();
@Override
public String convertToDatabaseColumn(List<Note> notes) {
return gson.toJson(notes);
}
@Override
public List<Note> convertToEntityAttribute(String string) {
return gson.fromJson(string, new TypeToken<List<Note>>(){}.getType());
}
}
And here is the field on the entity side :
@Column(name = "note", columnDefinition = "text")
@Getter @Setter
private List<Note> notes;
I can persist the entity with notes just fine, the problem is when I want to merge the entity, then the 'notes' field is never updated on the database : the database field is never updated, it does not even try to merge the entity as the optimist lock does not change.
Do you have any idea of what is happening ? Am I doing something wrong, is it supposed to be this way or is it a bug ?
Thank you !
I asked the same question on Eclipse's forums and got an answer, or rather the answer.
By default and as stated here, the list is considered immutable by eclipselink, this means that you can add or remove things to the list but as long as you don't assign a 'new' list to the field eclipselink is not going to try to merge it. So to make it work you either need to assign a new list, or you need to annotate the field with @Mutable.
New list example :
notes.add(newNote);
notes = new ArrayList<>(notes);
Annotation example :
@Mutable
@Column(name = "note", columnDefinition = "text")
@Getter @Setter
private List<Note> notes;
Thanks a lot to Chris Delahunt from eclipse's forum.