MorphiaOrg / MorphiaOrg/morphia
@PrePersist misses (partly) the point
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 449
- Avg merge
- 7m
- Merged PRs (30d)
- 10
Description
As supposed in the Wiki I implemented a lastChange timestamp. Now I found out by accidence that the @PrePersist annotated method is called many times more. If you have a look at the call chain it is clear why:

The prePersist-method is called everytime an object gets mapped to a DBObject. This is not totally bad since it works in most cases: The update doesn't care if the lastChange field is not persisted. Nevertheless I stumbled over following case where I ran into problems:
- There is an entity (Application) with an embedded list of children (UsageData).
- The children (UsageData) have a lastChange-field as shown in the Wiki.
- You want to remove a certain child from the list. Pseudocode (deriving from BasicDAO):
Query<Application> query = createQuery().field("_id").equal(applicationId);
UpdateOperations<Application> updateOps = createUpdateOperations();
updateOps.removeAll("usageDataList", usageDataToRemove);
update(query, updateOps)
- The remove of the child does never work since the lastChange-field gets updated in the callstack of removeAll. Problem is, that the database do only remove the embedded children which are field-by-field-compared equal. Since the lastChange field got updated by @PrePersist the usageData in the database and in the DBObject provided do always differ!
Is there an easy workaround?
(In my case I gonna try to avoid @PrePersist here...)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the LifecycleMethods wiki guidance and the BasicDAO update/removeAll path described in the issue. Trace where @PrePersist runs while embedded UsageData is mapped, then reproduce removal of a child whose lastChange changes during mapping. Done means the intended behavior or workaround is established so removeAll can match and remove the embedded child reliably.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, mongodb
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100