I've defined the following class as as default Entity Listener, so every time I call the persist() or merge() methods this code will be executed automatically:
public class StringProcessorListener {
@PrePersist
@PreUpdate
public void formatStrings(Object object) {
try {
for (Field f : object.getClass().getDeclaredFields()) {
if (f.getType().equals(String.class)) {
f.setAccessible(true);
if (f.get(object) != null) {
f.set(object, f.get(object).toString().toUpperCase());
}
}
}
} catch (Exception ex) {
Logger.getLogger(StringProcessorListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
The purpose of this is to capitalize all the strings of an object before insert it into the database. The @PrePersist worked fine, the method modifies all the strings and it is saved capitalized on the database, but when I try to update an object it didn't work so well, the method is called normally and it also modifies the strings, but instead of saving the modified object on the database, it is saving the object as it was before this method.
Any ideas on how to solve this?
Update:
I solved it using a DescriptorEvent
, it gave me access to the ObjectChangeSet and I could update it manually, then the values were correctly saved on the database.