@PrePersist method not invoked sometimes
We use @PrePersist to initialize few fields during Save of an entity. This feature seems to be working fine most of the times. But in rare cases, this PrePersist module code is never invoked. Also we dont have the fields initialized. This leads to many processing bugs due to missing field.
- Server Version: 4.2.x
- Driver Version: 4.4.0
- Morphia Version:Using Morphia 2.2.8. [ Same issue happened in 2.2.3 as well]
@evanchooly , please suggest why this would happen. No Errors on console as well. Is there a way to track why this is skipped?
It's hard to say without more details so a reproducer might help. You might also make sure you're explicitly mapping all your entities. It shouldn't matter. Morphia should dynamically pick those up along the way but there have been gaps over the years. I think I've plugged them all but I wouldn't rule out a gap that's been missed.
@evanchooly , is there a guideline on how to use EntityListener or PrePersist Annotations. Like, based on code, MorphiaCodecProvider will ensure LifeCycleEncoder be applied. Should CodecRegistry be registered at Database level or at Mapper level? Any documents to go over this please?
There's this page in the guide.
Thank you @evanchooly .. Is there a logger [on morphia] which could be used for analysis on why this is missed? Could you please share the areas where this was fixed, so could try to understand code and look out for other similar places
I don't have any specific changes to point you to, no, as those were done ages ago. There is also not much in the way of logging in general. If you could isolate a reproducer where this happens that would be immensely helpful.
@evanchooly , it is not consistently reproduceable.
- Added @prepersist method on @Entity annotated class. Should the LifecycleEncoder be specified anywhere explicitly during DataStore construction?
it looks like, sometimes, DAO makes use of LifecycleEncoder but sometimes it just uses Normal EntityEncoder. Any insights please on missing configuration stuff?
P.S: have collected heap dump. Anything that can be verified at Entity or DAO level to determine whats wrong?
Heap Dump screen shot of MorphiaCodecProvider for our Entity:

This show, LifecycleEncoder is registered for the EntityModel. BUt still the lifecycle method was not invoked.
Sorry, i dropped the ball on this one. any chance you could provide a reproducer? There's another issue involving lifecycle events and parent classes so you might be up against that one, too.
Confirm issue. Got the same problem. Didn't find any system or method of reproduction.