ErraiEntityManager: Missing event before merge
jannis Oct 18, 2013 11:30 AMHello,
is it possible to add an event that is fired before an entity gets merged into another one, or to change the existing PrePersist / -Update events to be fired on the entity that will be merged, instead of the one that has been merged?
This is necessary for us, as we need to prepare entities before they are persisted. More precisely, we have nested objects which are not known to the entity manager. We marshal these objects into a JSON String which is stored in a helper field:
@Entity public class ExampleEntity { @Transient private Nested nested; private String nestedHelper; @PrePersist private void prePersist() { nestedHelper = Marshalling.toJSON(nested); } @PreUpdate private void preUpdate() { nestedHelper = Marshalling.toJSON(nested); } @PostLoad private void postLoad() { nested = Marshalling.fromJSON(nestedHelper, ExampleEntity.class); } }
When a new version from this entity is received from the server, it is merged into the entity manager. The helper field of the received entity is null and after the merge the nested object is null as well. Our problem is, that the marshalling is not done, because neither PrePersist nor PreUpdate got called for the received entity. One of them is fired after the merge for the merged entity.
One solution might be the introduction of a new event to notify that an entity is about to be merged into another one. But I think that this would be inappropriate. The other events are general events from javax.persistence and one event should be enough.
My other idea would be to change the already existing PrePersist / -Update events to be fired before the merge and not for the merged entity but the entity that will be used as basis for the merge. I think, that this would rely to the documentation:
@PrePersist The entity is about to be persisted or merged into the entity manager. (Errai)
Problems with this idea might be that there are different objects getting notified by the PrePersist / -Update and the PostPersist / -Update events, the object being notified by the PrePersist / -Update events is not going to be persisted (directly) and that it is not managed.
Alternatively, we would have to ensure, that nested objects are marshalled before merge is called.
Are there any alternatives that I forgot to think of?
Thanks in advance
Jannis